%% Time-Synchronous Average of Sinusoid
% Compute the time-synchronous average of a noisy sinusoid.
%
% Generate a signal consisting of a sinusoid embedded in white Gaussian noise. The signal is sampled at 500 Hz for 20 seconds. Specify a sinusoid frequency of 10 Hz and a noise variance of 0.01. Plot one period of the signal.

fs = 500;
t = 0:1/fs:20-1/fs;

f0 = 10;
y = sin(2*pi*f0*t) + randn(size(t))/10;

plot(t,y)
xlim([0 1/f0])

% Compute the time-synchronous average of the signal. For the synchronizing signal, use a set of pulses with the same period as the sinusoid. Use tsa without output arguments to display the result.

tPulse = 0:1/f0:max(t);

tsa(y,fs,tPulse)