% DSP Homework 1
%
% T Kite February 9 1998


% Start by generating a time index and some signals.  Assume the sampling
% frequency is 8 kHz.  Plot the signals.

fs=8000;
ts=1/fs;
t=0:ts:0.02-ts;
[si,tr,pu]=stp(pi*t*400);
plot(t*1000,si,'--',t*1000,tr,'-.',t*1000,pu,'-')
axis([min(t*1000) max(t*1000) -1 1])
title 'Output of stp function'
xlabel 'Time (ms)'
ylabel 'Amplitude'
pause; clf

% Check the frequency content of the three signals and plot them.

[si_fr,w]=freqz(si,1,80,fs);
tr_fr=freqz(tr,1,80,fs);
pu_fr=freqz(pu,1,80,fs);

subplot(311); plot(w,abs(si_fr))
title 'Frequency content of sine wave'
xlabel 'Frequency (Hz)', ylabel 'Magnitude'
subplot(312); plot(w,abs(tr_fr))
title 'Frequency content of triangle wave'
xlabel 'Frequency (Hz)', ylabel 'Magnitude'
subplot(313); plot(w,abs(pu_fr))
title 'Frequency content of pulse wave'
xlabel 'Frequency (Hz)', ylabel 'Magnitude'
pause; clf

% Now generate a low pass filter, and plot its response.

f=remez(9, [0 400 800 4000]/4000, [1 1 0 0]);
[ff,w]=freqz(f,1,256,fs);
plot(w,abs(ff))
title 'Frequency response of filter'
xlabel 'Frequency'; ylabel 'Magnitude'
pause; clf

% Filter the three signals with the filter, and crop them to three
% full cycles.  Plot them.

si_f=conv(si,f);
tr_f=conv(tr,f);
pu_f=conv(pu,f);
si_c=si_f(26:145);
tr_c=tr_f(26:145);
pu_c=pu_f(26:145);
nt=(0:ts:(length(si_c)-1)*ts)*1000;

subplot(311); plot(nt,si_c); axis([min(nt) max(nt) -1.2 1.2])
title 'Filtered sine wave'
xlabel 'Time (ms)'; ylabel 'Amplitude'
subplot(312); plot(nt,tr_c); axis([min(nt) max(nt) -1 1])
title 'Filtered triangle wave'
xlabel 'Time (ms)'; ylabel 'Amplitude'
subplot(313); plot(nt,pu_c); axis([min(nt) max(nt) -0.2 0.2])
title 'Filtered pulse wave'
xlabel 'Time (ms)'; ylabel 'Amplitude'
pause; clf

% Compute the frequency content of the filtered signals and plot them.

[si_c_fr,w]=freqz(si_c,1,60,fs);
tr_c_fr=freqz(tr_c,1,60,fs);
pu_c_fr=freqz(pu_c,1,60,fs);

subplot(311); plot(w,abs(si_c_fr))
title 'Frequency content of filtered sine wave'
xlabel 'Frequency (Hz)', ylabel 'Magnitude'
subplot(312); plot(w,abs(tr_c_fr))
title 'Frequency content of filtered triangle wave'
xlabel 'Frequency (Hz)', ylabel 'Magnitude'
subplot(313); plot(w,abs(pu_c_fr))
title 'Frequency content of filtered pulse wave'
xlabel 'Frequency (Hz)', ylabel 'Magnitude'
