0% found this document useful (0 votes)
2 views4 pages

Lab 4

The document outlines a frequency analysis using the Discrete Fourier Transform (DFT) with two cases of signal lengths (N=40 and N=32) and two frequency components (500 Hz and 1000 Hz). It includes the generation of time-domain signals, computation of their DFTs, and visualization of both the time-domain signals and their magnitude spectra. Additionally, it demonstrates the application of a Hann window on signals and analyzes the effect of different window lengths on the frequency spectrum.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Lab 4

The document outlines a frequency analysis using the Discrete Fourier Transform (DFT) with two cases of signal lengths (N=40 and N=32) and two frequency components (500 Hz and 1000 Hz). It includes the generation of time-domain signals, computation of their DFTs, and visualization of both the time-domain signals and their magnitude spectra. Additionally, it demonstrates the application of a Hann window on signals and analyzes the effect of different window lengths on the frequency spectrum.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

% Frequency Analysis using the DFT

clc; clear; close all;

% Define parameters
fs = 8000; % Sampling frequency in Hz
N1 = 40; % Case 1: Signal length = 40
N2 = 32; % Case 2: Signal length = 32
f1 = 500; % First frequency component in Hz
f2 = 1000; % Second frequency component in Hz

% Generate time vectors


t1 = (0:N1-1) / fs;
t2 = (0:N2-1) / fs;

% Generate signals
x1 = sin(2 * pi * f1 * t1) + 0.05 * sin(2 * pi * f2 * t1); % Case 1: N=40
x2 = sin(2 * pi * f1 * t2) + 0.05 * sin(2 * pi * f2 * t2); % Case 2: N=32

% Compute DFTs
X1 = fft(x1, N1);
X2 = fft(x2, N2);

% Frequency axes
f1_axis = (-N1/2:N1/2-1) * (fs/N1);
f2_axis = (-N2/2:N2/2-1) * (fs/N2);

% Plot Case 1: Time-Domain Signal (N=40)


figure;
stem(t1, x1, 'r');
xlabel('Time (seconds)');
ylabel('x[n]');
title('Time-Domain Signal (N=40)');
grid on;

% Plot Case 1: Magnitude Spectrum (N=40)


figure;
stem(f1_axis, abs(fftshift(X1)), 'b');
xlabel('Frequency (Hz)');
ylabel('|X(f)|');
title('Magnitude Spectrum (N=40)');
grid on;

% Plot Case 2: Time-Domain Signal (N=32)


figure;
stem(t2, x2, 'r');
xlabel('Time (seconds)');
ylabel('x[n]');
title('Time-Domain Signal (N=32)');
grid on;

% Plot Case 2: Magnitude Spectrum (N=32)


figure;
stem(f2_axis, abs(fftshift(X2)), 'b');
xlabel('Frequency (Hz)');
ylabel('|X(f)|');
title('Magnitude Spectrum (N=32)');
grid on;
% Frequency Analysis using the DFT
clc; clear; close all;

% Define parameters
fs = 8000; % Sampling frequency in Hz
N1 = 40; % Case 1: Signal length = 40
N2 = 32; % Case 2: Signal length = 32
f1 = 500; % First frequency component in Hz
f2 = 1000; % Second frequency component in Hz

% Generate time vectors


t1 = (0:N1-1) / fs;
t2 = (0:N2-1) / fs;

% Generate signals
x1 = sin(2 * pi * f1 * t1) + 0.05 * sin(2 * pi * f2 * t1); % Case 1: N=40
x2 = sin(2 * pi * f1 * t2) + 0.05 * sin(2 * pi * f2 * t2); % Case 2: N=32

% Compute DFTs
X1 = fft(x1, N1);
X2 = fft(x2, N2);

% Frequency axes
f1_axis = (-N1/2:N1/2-1) * (fs/N1);
f2_axis = (-N2/2:N2/2-1) * (fs/N2);

% Plot Case 1: Time-Domain Signal (N=40)


figure;
stem(t1, x1, 'r');
xlabel('Time (seconds)');
ylabel('x[n]');
title('Time-Domain Signal (N=40)');
grid on;

% Plot Case 1: Magnitude Spectrum (N=40)


figure;
stem(f1_axis, abs(fftshift(X1)), 'b');
xlabel('Frequency (Hz)');
ylabel('|X(f)|');
title('Magnitude Spectrum (N=40)');
grid on;

% Plot Case 2: Time-Domain Signal (N=32)


figure;
stem(t2, x2, 'r');
xlabel('Time (seconds)');
ylabel('x[n]');
title('Time-Domain Signal (N=32)');
grid on;
% Plot Case 2: Magnitude Spectrum (N=32)
figure;
stem(f2_axis, abs(fftshift(X2)), 'b');
xlabel('Frequency (Hz)');
ylabel('|X(f)|');
title('Magnitude Spectrum (N=32)');
grid on;

% Apply Hann window


hann_window = hann(N)';
x_win = x .* hann_window;

% Compute DFT with windowing


X_win = fft(x_win);

% Plot windowed time-domain signal


figure;
stem(t * 1e6, x_win, 'g'); % Time in microseconds
xlabel('Time (\mus)');
ylabel('Amplitude');
title('Time-Domain Signal with Hann Window');
grid on;

% Plot magnitude spectrum with windowing


figure;
stem(f_axis, abs(fftshift(X_win)), 'm');
xlabel('Frequency (Hz)');
ylabel('|X(f)|');
title('Magnitude Spectrum (With Hann Window)');
grid on;
% Define three closely spaced frequencies
w0 = 0.2 * pi;
w1 = 0.22 * pi;
w2 = 0.6 * pi;

% Generate signal with different window lengths


L_values = [25, 50, 100];

for i = 1:length(L_values)
L = L_values(i);
n = 0:L-1; % Time index
x_signal = cos(w0*n) + cos(w1*n) + cos(w2*n);

% Apply Hann window


win = hann(L)';
x_win = x_signal .* win;

% Compute DFT
X_spectrum = fft(x_win, 256); % Zero-padding to 256 points
f_axis = (-128:127) * (fs/256); % Adjust frequency axis

% Plot spectrum for different L values


figure;
stem(f_axis, abs(fftshift(X_spectrum)), 'k');
xlabel('Frequency (Hz)');
ylabel('|X(f)|');
title(['Magnitude Spectrum (L = ', num2str(L), ')']);
grid on;
end

You might also like