DSP Lab4
DSP Lab4
SOFTWARE ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI
SUBMITTED TO:
LE Sundas
SUBMITTED BY:
Shaheer Mukhtiar
Reg # 432017
DE- 44 Dept CE
Code:
f = 10;
Fs1 = 2*f;
Fs2 = 1.5*f;
Fs3 = 3*f;
t = 0:0.001:1;
% Continuous signal
Y = cos(2*pi*f*t);
% Sampled signals
t1 = 0:1/Fs1:1;
Y1 = cos(2*pi*f*t1);
t2 = 0:1/Fs2:1;
Y2 = cos(2*pi*f*t2);
t3 = 0:1/Fs3:1;
Y3 = cos(2*pi*f*t3);
% Plot
figure;
subplot(4,1,1);
stem(t1, Y1);
title('Sampled at Fs = 2Fc = Hz');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(4,1,2);
stem(t2, Y2);
title('Sampled at Fs < 2Fc = Hz');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(4,1,3);
stem(t3, Y3);
title('Fs > 2Fc = Hz');
xlabel('Time (s)');
ylabel('Amplitude');
Output:
Part b
Code:
f2 = 50;
f3 = 200;
Fs1 = 2 * f3;
Fs2 = f3 - 50;
Fs3 = 2.5 * f3;
t = 0:0.001:0.1;
% Continuous signal
Y = sin(2 * pi * f2 * t) + cos(2 * pi * f3 * t);
% Sampled signals
t1 = 0:1/Fs1:0.1;
Y1 = sin(2 * pi * f2 * t1) + cos(2 * pi * f3 * t1);
t2 = 0:1/Fs2:0.1;
Y2 = sin(2 * pi * f2 * t2) + cos(2 * pi * f3 * t2);
t3 = 0:1/Fs3:0.1;
Y3 = sin(2 * pi * f2 * t3) + cos(2 * pi * f3 * t3);
% Plot
figure;
subplot(4,1,2);
stem(t1, Y1);
title('Sampled at Fs = 2Fc = Hz');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(4,1,3);
stem(t2, Y2);
title('Sampled at Fs < 2Fc = Hz');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(4,1,4);
stem(t3, Y3);
title('Sampled at Fs > 2Fc = Hz');
xlabel('Time (s)');
ylabel('Amplitude');
Output:
Task 2:
Code:
Fs = 44100;
duration = 5;
y = getaudiodata(recording);
L = length(y);
Y = fft(y);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
f = Fs*(0:(L/2))/L;
Fs_Nyquist = 2 * peak_freq;
Fs_Under = peak_freq - 50;
Fs_Over = 2.5 * peak_freq;
subplot(4,1,3);
stem(t_Under, y_Under, '.');
title('Sampling at Fs < 2Fc (Aliasing Effect)');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(4,1,4);
stem(t_Over, y_Over, '.');
title('Sampling at Fs > 2Fc (Over-sampling)');
xlabel('Time (s)');
ylabel('Amplitude');
Output: