DEPARTMENT OF COMPUTER &
SOFTWARE ENGINEERING
COLLEGE OF E&ME, NUST, RAWALPINDI
EC312 Digital Signal Processing
Lab Number: 4
SUBMITTED TO:
LE Sundas
SUBMITTED BY:
Shaheer Mukhtiar
Reg # 432017
DE- 44 Dept CE
Submission Date: 4 March 2025
LAB # 04: Sampling Audio Signals
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;
recording = audiorecorder(Fs, 16, 1);
disp("Recording...");
recordblocking(recording, duration);
disp("Recording stopped.");
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;
% Find dominant frequency
[max_val, idx] = max(P1);
peak_freq = f(idx);
% Plot Frequency Spectrum
subplot(4,1,1)
plot(f, P1);
title('Frequency Spectrum of Recorded Audio');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
fprintf('Dominant Frequency: %.2f Hz\n', peak_freq);
Fs_Nyquist = 2 * peak_freq;
Fs_Under = peak_freq - 50;
Fs_Over = 2.5 * peak_freq;
[p1, q1] = rat(Fs_Nyquist / Fs);
[p2, q2] = rat(Fs_Under / Fs);
[p3, q3] = rat(Fs_Over / Fs);
y_Nyquist = resample(y, p1, q1);
y_Under = resample(y, p2, q2);
y_Over = resample(y, p3, q3);
t_Nyquist = (0:length(y_Nyquist)-1) / (Fs * p1 / q1);
t_Under = (0:length(y_Under)-1) / (Fs * p2 / q2);
t_Over = (0:length(y_Over)-1) / (Fs * p3 / q3);
subplot(4,1,2);
stem(t_Nyquist, y_Nyquist, '.');
title('Sampling at Fs = 2Fc (Nyquist Rate)');
xlabel('Time (s)');
ylabel('Amplitude');
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: