1.0 Objectives:
2.0 Procedure:
Be sure to document your program. The first
few lines should include the program name, your name, the date, and a brief
description of what this program does. Intersperse comments in appropriate
places throughout the program. Remember, comments are preceded with
a %
fs = ;
% fs is the sampling frequency. Try 1,2, or 4
dt = ; % dt is the time between sampled points (inverse of fs)
N = ; % N is the number of sampled points. Try 128 or 256.
NS = ; % NS is the number of samples (for Lab 4, set NS=1)
T = N*dt; % T is the sampling period
df = 1/T; % df is the fundamental frequency
Nf= N/2 + 1; % Nf is the index of the Nyquist frequency
fn = ??
% the Nyquist frequency - what is it?
y1=A*cos(2*pi*f*t - phase) + off;
% generate a sinusoid vector
y2 = randn(size(y1));
% generate Gaussian noise vector, same size as y1
% (note that it won't be perfectly Gaussian, since
% y1 contains only a finite number of points)
y2= y2*sd + xmean; % Set standard deviation and offset of noise.
ydata=y1 + y2; % this is your synthetic data!
In creating y1, you'll also need to select values for off, the mean value or offset, A, the amplitude, f, the sinusoid's frequency, and phase, the angle in radians. Be careful what value you use for f so you don't get leakage or aliasing. Before generating y2, assign values to the constants sd and xmean. The values you choose will become the standard deviation and offset, respectively, of the random y2 data.
figure(1) % open 1st graphics window
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Stop here, run your m-file and check that the synthetic
data is what you expect.
How would you do this?
Take advantage of matlab's interactive features
by checking your code frequently.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
X = fft(ydata);
% two-sided matlab amplitude spectrum (see backgrd info sheet)
X = X/N;
% convert to our form of amplitude spectrum
% (see backgrd info sheet - discrete equations)
X(Nf+1:N, :) = [];
% Create single-sided spectrum by folding (i.e., deleting freq>fn
Ax = 2*X(2:Nf, :);
% and doubling the magnitude of remaining components; leave out freq=0)
When matlab performs an FFT, it returns a double-sided spectrum
of N amplitude values corresponding to frequencies f1 (= 0*df = 0)
through fN (= (N-1)*df). Matlab's
amplitude values are symmetric about fn, the Nyquist frequency, rather than zero frequency as
you've seen in class. The above folding calculation will result in a
single-sided spectrum running from df to fn.
Gx = abs(Ax) .^2/(2*df);
% the single-sided energy density spectrum from df to fn (Note `.^')
Gxmean=mean(Gx,2);
% Gxmean becomes a column vector (average over the ensembles)
3.0 Report: