Lab 7 DSP
Lab 7 DSP
SOFTWARE ENGINEERING
COLLEGE OF E&ME, NUST,
RAWALPINDI
SUBMITTED TO:
LE Sundas
SUBMITTED BY:
Shaheer Mukhtiar
Reg # 432017
DE- 44 Dept CE
Code:
%% Task 1
T= 2;
fs = 100;
t = 0:1/fs:T;
sinosoid = sin(2*pi*80*t);
X_f = fft(sinosoid);
N = length(X_f);
frequencies = (0:N-1) * (fs / N);
magnitude = abs(X_f);
phase = angle(X_f);
figure;
subplot(2, 1, 1)
plot(frequencies, fftshift(magnitude))
ylabel("Amplitude")
xlabel("Samples")
title("Magnitude Plot")
subplot(2, 1, 2)
plot(frequencies, phase)
ylabel("Angle")
xlabel("Samples")
title("Phase Plot")
Output:
Task 2: Analyzing Signal Component
Objective: Analyze a composite signal using the Fourier Transform.
Steps:
1. Create a composite signal by adding multiple sinusoids of different frequencies.
2. Compute the FFT of the composite signal.
3. Identify the frequency components from the magnitude spectrum.
Code:
%% Task 2
duration = 2;
sampling_rate = 1000;
time = 0:1/sampling_rate:duration;
freq1 = 5;
freq2 = 50;
freq3 = 100;
signal1 = sin(2*pi*freq1*time);
signal2 = sin(2*pi*freq2*time);
signal3 = sin(2*pi*freq3*time);
magnitude_spectrum = abs(fft_result);
phase_spectrum = angle(fft_result);
subplot(2, 1, 1)
plot(freq_axis, fftshift(magnitude_spectrum))
ylabel("Amplitude")
xlabel("Frequency (Hz)")
title("Magnitude Spectrum")
subplot(2, 1, 2)
plot(freq_axis, phase_spectrum)
ylabel("Angle (Radians)")
xlabel("Frequency (Hz)")
title("Phase Spectrum")
Output:
Task 3: Filtering out noise
Objective: Filter a noisy signal using Lowpass band filter.
Steps:
1. Generate cosine signal in such a way that first value should be 0 with frequency 5Hz,
Amplitude 5 and Fs=5000.
2. Compute Fourier Transform of respective signal using MATLAB command fft() or user
defined function. Plot the signal in frequency domain
3. Add Gaussian noise in input signal using (Y =awgn(x,10,'measured')) command. Here x
is input signal. Plot the resultant signal in time domain.
4. Pass DTFT of input signal from LTI system (Lowpass band) analyze the results.
5. Plot the resultant signal in time domain.
Code:
%% Task 3
freq = 5;
sampling_rate = 5000;
amplitude = 5;
duration = 2;
time = 0:1/sampling_rate:duration;
fft_result = fft(cosine_signal);
magnitude_spectrum = abs(fftshift(fft_result));
num_samples = length(fft_result);
filter_order = 4;
cutoff_freq = 1000 / (sampling_rate / 2);
[b, a] = butter(filter_order, cutoff_freq);
filtered_signal = filter(b, a, noisy_signal);
subplot(3, 1, 1)
plot(magnitude_spectrum)
ylabel("Amplitude")
xlabel("Samples")
title("Fourier Magnitude of Cosine Signal")
subplot(3, 1, 2)
plot(time, noisy_signal)
ylabel("Amplitude")
xlabel("Time (s)")
title("Noisy Signal")
subplot(3, 1, 3)
plot(time, filtered_signal)
ylabel("Amplitude")
xlabel("Time (s)")
title("Filtered Signal")
Output:
Task 4:
%% Task 4
n = -8:8;
h_n = [0 0 0 0 0 0 1 2 3 2 1 0 0 0 0 0 0];
subplot(2,1,1);
plot(w/pi, abs(h));
title('Magnitude Response');
xlabel('Normalized Frequency');
ylabel('|H(w)|');
subplot(2,1,2);
plot(w/pi, angle(h));
title('Phase Response');
xlabel('Normalized Frequency');
ylabel('Phase (radians)');
Output:
Task 5: Real-World Signal Analysis
Objective: Apply the Fourier Transform to a real-world signal (e.g., audio, ECG).
Steps:
1. Load a real-world signal (e.g., an audio file or ECG data).
2. Compute the FFT and analyze the frequency components.
3. Identify dominant frequencies and their significance.
Code:
%% Task 5
N = length(signal);
Y = fft(signal);
f = linspace(-Fs/2, Fs/2, N);
magnitude = abs(fftshift(Y))/N;
[~, peakIndex] = max(magnitude);
dominantFreq = f(peakIndex);
subplot(2,1,1);
t = (0:N-1)/Fs;
plot(t, signal);
title('Time-Domain Signal');
xlabel('Time (seconds)');
ylabel('Amplitude');
subplot(2,1,2);
plot(f, magnitude);
title('Frequency-Domain Signal (Magnitude Spectrum)');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;
Output:
Dominant Freq = -229.13
Task 6: You have to design the ideal low pass filter with fixed length approximation of IIR
Filter, by using different values of M. Sample output is given below. Find h[n] for each, make it
causal and plot the frequency, phase, and magnitude response of the system.
Code:
%% Task 6
clc; clear; close all;
figure;
for idx = 1:length(M_values)
filter_length = M_values(idx);
time_index = -filter_length:filter_length;
impulse_response = zeros(size(time_index));
impulse_response(time_index~=0) = sin(cutoff_freq *
time_index(time_index~=0)) ./ (pi * time_index(time_index~=0));
impulse_response(time_index==0) = cutoff_freq / pi;
subplot(2,2,idx);
plot(freq_axis - pi, abs(fftshift(freq_response)), 'LineWidth', 1.5);
title(['Magnitude Response for M = ', num2str(filter_length)]);
xlabel('Frequency (rad/sample)');
grid on;
end
Output: