0% found this document useful (0 votes)
13 views

Lab 7 DSP

This document outlines a lab report for EC312 Digital Signal Processing, detailing six tasks involving the Fourier Transform. Each task includes objectives, steps, and MATLAB code for visualizing and analyzing signals, filtering noise, and applying the Fourier Transform to real-world data. The tasks cover generating sinusoidal signals, analyzing composite signals, filtering techniques, and designing an ideal low-pass filter.

Uploaded by

shaheerkz12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lab 7 DSP

This document outlines a lab report for EC312 Digital Signal Processing, detailing six tasks involving the Fourier Transform. Each task includes objectives, steps, and MATLAB code for visualizing and analyzing signals, filtering noise, and applying the Fourier Transform to real-world data. The tasks cover generating sinusoidal signals, analyzing composite signals, filtering techniques, and designing an ideal low-pass filter.

Uploaded by

shaheerkz12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

DEPARTMENT OF COMPUTER &

SOFTWARE ENGINEERING
COLLEGE OF E&ME, NUST,
RAWALPINDI

EC312 Digital Signal Processing


Lab Number: 7

SUBMITTED TO:
LE Sundas

SUBMITTED BY:
Shaheer Mukhtiar
Reg # 432017
DE- 44 Dept CE

Submission Date: 18 March 2025


Lab # 07: Fourier Transform
Task 1: Understanding the Fourier Transform
Objective: Visualize the Fourier Transform of simple signals
Steps:
1. Generate a simple sinusoidal signal (e.g., x(t)=sin(2πft)
2. Compute its Fourier Transform using the FFT algorithm.
3. Plot the magnitude spectrum and phase spectrum.

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);

combined_signal = signal1 + signal2 + signal3;


fft_result = fft(combined_signal);
num_samples = length(fft_result);
freq_axis = (0:num_samples-1) * (sampling_rate / num_samples);

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;

cosine_signal = amplitude * cos((2*pi*freq*time) - pi/2);

fft_result = fft(cosine_signal);
magnitude_spectrum = abs(fftshift(fft_result));
num_samples = length(fft_result);

noisy_signal = awgn(cosine_signal, 10, 'measured');

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:

Consider an LTI system with an even unit sample response.

1. Plot the Frequency response of this filter


2. Plot the phase response of this filter
3. Plot the magnitude response of this filter
Code:

%% Task 4
n = -8:8;
h_n = [0 0 0 0 0 0 1 2 3 2 1 0 0 0 0 0 0];

[h, w] = freqz(h_n, 1, 1024);

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

filepath = "C:\Users\Shaheer\Documents\MATLAB\Lab 6\Record1.wav";


[signal, Fs] = audioread(filepath);

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;

disp(['Dominant Frequency: ', num2str(dominantFreq), ' Hz']);

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;

M_values = [5, 9, 15, 35];


cutoff_freq = pi/4;
fft_size = 512;

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;

hamming_window = 0.54 - 0.46 * cos(2 * pi * (time_index +


filter_length) / (2 * filter_length));
windowed_response = impulse_response .* hamming_window;
causal_response = [windowed_response, zeros(1, filter_length)];

[freq_response, freq_axis] = freqz(causal_response, 1, fft_size,


'whole');

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:

You might also like