You are on page 1of 7

MATLAB Problem Set #10

Spring 2013

% hmwk10.m % % Commands to produce the results for MATLAB Set #10. fs_fast=1000; % Create the original waveform using a fast sampling frequency. Ts_fast=1/fs_fast; % Compute the sampling period (0.001) for this fast sampling rate. t_final=1; % Define the time horizon to be one second long. time=0:Ts_fast:t_final; % Create a one second time interval for the original waveform. xf=sin(15*(2*pi)*time)+sin(40*(2*pi)*time); % Original waveform: 15 Hz and 40 Hz comp. figure(1) plot(time,xf) % Examine the original waveform in the time domain. title('Original Waveform with 10 Hz and 40 Hz Components') Xf=fft(xf); % Compute the FFT (frequency response) for this original waveform. % I have selected the time horizon (1 second) and the sampling frequency (1000 Hz) % to make interpreting the FFT results a little easier. The FFT will provide the frequency % content of our signals. faxis=0:(1/t_final):(1/Ts_fast); % Define the frequency axis for this fast sampling case. figure(2) stem(faxis,abs(Xf)), % Examine the frequency content in the original waveform. title('Frequency Response of the Original Waveform') % What happens if we do not sample fast enough? % What if we only sample at 50 Hz? % To capture the high frequency component we would need to sample faster than 80 Hz! fs_slow=50; % Define a slow sampling frequency of 50 Hz. Ts_slow=(1/fs_slow); % Compute the slow sample period (0.02). time_slow=0:Ts_slow:t_final; % Create a time axis using this longer sample period. xs=sin(15*(2*pi)*time_slow)+sin(40*(2*pi)*time_slow); % Sample waveform slower. figure(3) plot(time_slow,xs), % Does this waveform look at all like the original waveform? title('Original Waveform with a Slow Sampling Frequency') % What happened to the high frequency component? % Did the high frequency component become aliased to a lower frequency? % Does this slow-sampled waveform appear to have another low frequency component? % What if we examine this slow-sampled waveform’s frequency content? faxis_slow=0:(1/t_final):(1/Ts_slow); % Create a new frequency axis based on Ts_slow. Xs=fft(xs); % Compute the FFT (find the frequency content) in this new signal. figure(4) stem(faxis_slow,abs(Xs)), % Is the frequency content consistent with your calculations? title('FFT of the Slowly Sampled Waveform')

% What low frequency components are present in this slow-sampled waveform? % For a sampling rate of 50 Hz, which produces a folding frequency of 25 Hz, does the % frequency content look consistent with your analysis? % Only concentrate on the frequencies below the folding frequency. % % You do not need to complete the rest of this material. % I am providing this material for your entertainment. % % You could examine the data more closely, if you wanted to manually display the % frequency and magnitude of the frequency response together. % % For the fast sampling: [faxis(1:50);abs(Xf(1:50))], % Examine the frequency content for first 49 Hz of original. % % For the slow sampling: [faxis_slow(1:20);abs(Xs(1:20))], % Examine the first 19 Hz of the slowsampled waveform. % % MATLAB has a command that will allow one to reduce the sample-rate without introducing % as much aliasing. The command is called ‘decimate’. The ‘decimate’ command performs a % low-pass filter on the original signal before the sampling is reduced! % Suppose we use the ‘decimate’ command and still reduce the sampling rate. % Let’s reduce the sampling rate by a factor of 20, from 1000 Hz to 50 Hz. % To learn more about the ‘decimate’ command, just type “help decimate” xs2=decimate(xf,20); % Extract every 20th data point in the fast-sampled waveform. time_slow2=decimate(time,20); % Extract the consistent set of time values. figure(5) stem(time_slow2,xs2), % Examine the waveform with anti-aliasing filtering performed. title('Sampled Original Waveform after Passing Through Anti-Aliasing Filter') hold on, % I will compare this with the original low frequency component. plot(time,sin(15*(2*pi)*time)), % Low-frequency component of original. stem(time_slow2,xs,'k'), % Go ahead and examine the aliased waveform. (Black 'k') legend('Anti-Aliased Filtered Original','Low-Frequency Component','Aliased Waveform') % It should be clear that the aliased waveform contains additional frequency content. % Thus, one should pre-filter, with an anti-aliasing filter, prior to sampling a waveform. % If one does not pre-filter, there is the risk that high frequency content will be aliased % down to lower frequencies, which can produce undesired results!

Figure 1 (below):

Original Waveform with 10 Hz and 40 Hz Components 2 1.5 1 0.5 0 -0.5 -1 -1.5 -2

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Figure 2 (below):

Frequency Response of the Original Waveform 600

500

400

300

200

100

0

0

100

200

300

400

500

600

700

800

900

1000

Figure 3 (below):

Original Waveform with a Slow Sampling Frequency 2 1.5 1 0.5 0 -0.5 -1 -1.5 -2

0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

Figure 4 (below):

FFT of the Slowly Sampled Waveform 25

20

15

10

5

0

0

5

10

15

20

25

30

35

40

45

50

Figure 5 (below):

Sampled Original Waveform after Passing Through Anti-Aliasing Filter 2 1.5 1 0.5 0 -0.5 -1 -1.5 -2 -0.2 Anti-Aliased Filtered Original Low-Frequency Component Aliased Waveform

0

0.2

0.4

0.6

0.8

1

1.2