Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
11 views12 pages

DSP Exp 8

The document outlines an experiment aimed at studying spectral leakage caused by the Discrete Fourier Transform (DFT) and the effects of various windowing functions on this phenomenon. It explains the causes of spectral leakage, the importance of windowing, and provides MATLAB code for implementing different windowing techniques and analyzing their impact on frequency resolution and leakage. Observations from the experiment highlight trade-offs between resolution and leakage suppression for various window types, emphasizing the significance of window selection in signal processing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views12 pages

DSP Exp 8

The document outlines an experiment aimed at studying spectral leakage caused by the Discrete Fourier Transform (DFT) and the effects of various windowing functions on this phenomenon. It explains the causes of spectral leakage, the importance of windowing, and provides MATLAB code for implementing different windowing techniques and analyzing their impact on frequency resolution and leakage. Observations from the experiment highlight trade-offs between resolution and leakage suppression for various window types, emphasizing the significance of window selection in signal processing.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

23102128 A6 Vansh Ahuja

Experiment No. 8

Aim: Develop a MATLAB program to study spectral leakage caused by the DFT and how
windowing can control its effects.
Theory:
Spectral leakage refers to the spreading of a signal’s energy across multiple frequency bins in
the frequency domain, causing distortion in the spectral representation. This phenomenon
occurs when a signal is truncated in the time domain, leading to discontinuities that create
spurious frequency components. There are different reasons that can lead to be the occurrence
of spectral leakage, some of them are mentioned below:
1. Non-Periodic Signals in the Observation Window: If the signal being analyzed is
not periodic within the observation window, the abrupt truncation introduces
discontinuities at the edges. These discontinuities cause the frequency spectrum to
spread beyond the actual frequency components of the signal.
2. Finite Observation Window: Fourier Transform assumes infinite signal duration. In
practical applications (like the DFT), signals are analyzed over a finite duration. This
truncation effectively multiplies the signal by a rectangular window, which has its
own spectral characteristics that spread the energy.
3. Mismatched Sampling and Frequency: If the signal’s frequency does not align with
the discrete Fourier bins (as determined by sampling rate and window length), leakage
occurs because the signal’s energy does not fit neatly into one frequency bin.
Due to spectral leakage, the signal’s energy spreads into nearby frequency bins, making it
difficult to identify the exact signal frequency. This also reduces the ac- curacy of power
spectrum estimation. Spectral leakage can be mitigated by using the DTFT of the signal. In
DTFT, longer discrete-time sequences can be generated for the same sampling duration by
increasing the sampling frequency. However, this does not completely eliminate the problem.
To address this, data windowing plays a crucial role in reducing the artificial high frequencies
introduced in the DFT due to finite-length sampling.
Windowing: When the DFT is applied to an aperiodic signal it is practical to just take
window of the sequence. A window region can be defined by effectively multiplying the
signal x(n) by a rectangular window w(n), as shown in figure 1. The windowed function of
the signal xN (n) can be mathematically defined by equation:
xN (n) = x(n) ∗ w(n) (1)

The rectangular window function is defined by the following parameters:


w(n) = 1, n1 ≤ n ≤ n2 (2)
0, otherwise
The time domain and frequency domain representation of the rectangular function is
illustrated in figures 2 (a) and (b) respectively. With reference of figure (b), the Fourier
transform of a rectangular window is well known sinc function.

59
23102128 A6 Vansh Ahuja

When obtaining the spectral representation of a signal using the Discrete Fourier Transform
(DFT), windowing functions impact spectral leakage and frequency resolution.
Effect of Different Windowing Functions
• Rectangular Window: Provides the best frequency resolution but results in high
spectral leakage due to sharp transitions.
• Hanning & Hamming Windows: Reduce leakage by tapering the signal at the edges
but slightly widen the main lobe, reducing frequency resolution.
• Blackman Window: Further suppresses leakage at the cost of a broader main lobe.
• Kaiser Window: Offers adjustable trade-offs between main lobe width and side lobe
attenuation by tuning the shape parameter
Effect of Window Length
1. Increasing Window Length:
• Improves frequency resolution as DFT bin spacing (∆ f=Fs/N) decreases.
• Reduces spectral leakage due to better signal representation.
2. Decreasing Window Length:
• Decreases frequency resolution, making it harder to distinguish close frequencies.
• Increases spectral leakage, causing undesired spectral spreading. A balance between
window function and length is essential based on the application, such as speech
analysis (short windows) or high-resolution spectral estimation (long windows).

IN LAB EXERCISE
1.
% Define parameters
fs = 1000; % Sampling frequency in Hz
N = 64; % Number of samples
t = (0:N-1) / fs; % Time samples (n * Ts)

% Generate sinusoids
x1 = 100 * sin(2 * pi * 101.56 * t);
x2 = 2 * sin(2 * pi * 156.25 * t);

% Display the results


disp('x1 buffer:');
disp(x1);
disp('x2 buffer:');
disp(x2);

figure;
subplot(2, 1, 1);
plot(t, x1);
title('x1(t) = 100sin(2\pi101.56t) [23102128]');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(2, 1, 2);
plot(t, x2);
title('x2(t) = 2sin(2\pi156.25t) [23102128]');
xlabel('Time (s)');

60
23102128 A6 Vansh Ahuja

ylabel('Amplitude');

2.
X1_fft = fft(x1,64);
X2_fft = fft(x2,64);
freq = (0:N-1)*(fs/N); % Frequency bins
figure;
subplot(2,1,1);
plot(freq, abs(X1_fft));
xlim([0 500]);
title('FFT of x1 using Rectangular Window [23102128]');

subplot(2,1,2);
plot(freq, abs(X2_fft));
xlim([0 500]);
title('FFT of x2 using Rectangular Window [23102128]');

61
23102128 A6 Vansh Ahuja

3.

X1_fft_1024 = fft(x1,1024);

X2_fft_1024 = fft(x2,1024);

freq_1024 = (0:1023)*(fs/1024);

figure;
subplot(2,1,1);
plot(freq_1024, abs(X1_fft_1024));
xlim([0 500]);
title('FFT of x1 using 1024-point FFT [23102128]');

subplot(2,1,2);
plot(freq_1024, abs(X2_fft_1024));
xlim([0 500]);
title('FFT of x2 using 1024-point FFT [23102128]');

4.
N_vals = [64, 128, 256];

for N = N_vals
w_hanning = hanning(N);
w_hamming = hamming(N);
w_blackman = blackman(N);
w_kaiser = kaiser(N,4); % Shape parameter 4

figure;
subplot(4,1,1);
plot(w_hanning);
title(['Hanning Window (N=' num2str(N) ') [23102128]']);

subplot(4,1,2);
plot(w_hamming);
title(['Hamming Window (N=' num2str(N) ') [23102128]']);

subplot(4,1,3);
plot(w_blackman);

62
23102128 A6 Vansh Ahuja

title(['Blackman Window (N=' num2str(N) ') [23102128]']);

subplot(4,1,4);
plot(w_kaiser);
title(['Kaiser Window (N=' num2str(N) ') [23102128]']);
end

Observations & Analysis

 Rectangular Window: Best frequency resolution but extreme spectral leakage.


 Bartlett & Hanning: Good trade-off for general spectral analysis.
 Hamming: Reduces leakage better than Hanning, useful for speech/audio.
 Blackman: High attenuation but significantly widens the main lobe (reducing
resolution).
 Kaiser (β-controlled): Adjustable trade-off—higher β provides better leakage
suppression at the cost of resolution.

63
23102128 A6 Vansh Ahuja

POST LAB EXERCISE


1.
a)
fs = 1000; % Sampling frequency in Hz
N = 64; % Length of buffer
t = (0:N-1)/fs; % Time vector

x1 = 100*sin(2*pi*101.56*t);

% Ensure the window is a row vector for correct element-wise multiplication


w_hanning = hanning(N)';

x1_hanning = x1 .* w_hanning; % Apply windowing

% Compute FFT
X1_hanning_fft = fft(x1_hanning, N);

% Frequency bins
freq = (0:N-1)*(fs/N);

% Plot the FFT result


figure;
stem(freq, abs(X1_hanning_fft));
xlim([0 500]);
title('FFT of x1 with Hanning Window [23102128]');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

b)
fs = 1000; % Sampling frequency in Hz
N = 1024; % Length of buffer
t = (0:N-1)/fs; % Time vector

64
23102128 A6 Vansh Ahuja

x1 = 100*sin(2*pi*101.56*t);

% Ensure the window is a row vector for correct element-wise multiplication


w_hanning = hanning(N)';

x1_hanning = x1 .* w_hanning; % Apply windowing

% Compute FFT
X1_hanning_fft = fft(x1_hanning, N);

% Frequency bins
freq = (0:N-1)*(fs/N);

% Plot the FFT result


figure;
stem(freq, abs(X1_hanning_fft));
xlim([0 500]);
title('FFT of x1 with Hanning Window [23102128]');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

2.
fs = 1000; % Sampling frequency in Hz
N = 64; % Buffer length
N_fft = 1024; % FFT length

t = (0:N-1)/fs; % Time vector

% Define individual sine waves


x1 = 100*sin(2*pi*101.56*t);
x2 = 2*sin(2*pi*156.25*t);

% Combine the two signals


x_combined = x1 + x2;

% Define Rectangular and Hamming Windows

65
23102128 A6 Vansh Ahuja

w_rect = ones(1, N); % Rectangular Window


w_hamming = hamming(N)'; % Hamming Window (Transpose to match signal shape)

% Apply Windows to Combined Signal


x_comb_rect = x_combined .* w_rect;
x_comb_hamming = x_combined .* w_hamming;

% Compute FFTs for both windowed signals


X_comb_rect_64 = fft(x_comb_rect, N);
X_comb_hamming_64 = fft(x_comb_hamming, N);
X_comb_rect_1024 = fft(x_comb_rect, N_fft);
X_comb_hamming_1024 = fft(x_comb_hamming, N_fft);

% Define frequency bins


freq_64 = (0:N-1)*(fs/N);
freq_1024 = (0:N_fft-1)*(fs/N_fft);

figure;

% 64-point FFT with Rectangular Window


subplot(2,2,1);
stem(freq_64, abs(X_comb_rect_64));
xlim([0 500]);
title('64-Point FFT with Rectangular Window [23102128]');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

% 64-point FFT with Hamming Window


subplot(2,2,2);
stem(freq_64, abs(X_comb_hamming_64));
xlim([0 500]);
title('64-Point FFT with Hamming Window [23102128]');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

% 1024-point FFT with Rectangular Window


subplot(2,2,3);
stem(freq_1024, abs(X_comb_rect_1024));
xlim([0 500]);
title('1024-Point FFT with Rectangular Window [23102128]');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

% 1024-point FFT with Hamming Window


subplot(2,2,4);
stem(freq_1024, abs(X_comb_hamming_1024));
xlim([0 500]);
title('1024-Point FFT with Hamming Window [23102128]');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

66
23102128 A6 Vansh Ahuja

3.
% Given frequencies
f1 = 101.56;
f2 = 156.25;

% Compute LCM of frequencies (to avoid spectral leakage)


new_fs = lcm(round(f1*100), round(f2*100)) / 100; % Convert back to original scale
fprintf('Minimum required sampling frequency: %.2f Hz\n', new_fs);

fs_new = new_fs; % Use computed sampling frequency


N = 64; % Buffer size
t_new = (0:N-1)/fs_new; % Adjusted time vector

% Generate new sine waves


x1_new = 100*sin(2*pi*f1*t_new);
x2_new = 2*sin(2*pi*f2*t_new);

% Combine the signals


x_combined_new = x1_new + x2_new;

% Compute FFTs
X_comb_new_64 = fft(x_combined_new, N);

% Frequency bins
freq_new = (0:N-1)*(fs_new/N);

% Plot the results


figure;
stem(freq_new, abs(X_comb_new_64));
title('FFT of Combined Signal with Corrected Sampling Frequency [23102128]');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

67
23102128 A6 Vansh Ahuja

4.
fs = 1000; % Sampling frequency in Hz
N = 64; % Buffer size

t = (0:N-1)/fs; % Time vector

% Generate signal (example using x1)


x1 = 100*sin(2*pi*101.56*t);

% Define different window functions


w_hanning = hanning(N)';
w_hamming = hamming(N)';
w_blackman = blackman(N)';
w_kaiser = kaiser(N, 4)'; % Kaiser with shape parameter = 4
w_flat_top = flattopwin(N)'; % Flat-Top window

% Apply windows
x1_hanning = x1 .* w_hanning;
x1_hamming = x1 .* w_hamming;
x1_blackman = x1 .* w_blackman;
x1_kaiser = x1 .* w_kaiser;
x1_flat_top = x1 .* w_flat_top;

% Compute FFTs for each windowed signal


X_hanning_fft = fft(x1_hanning, N);
X_hamming_fft = fft(x1_hamming, N);
X_blackman_fft = fft(x1_blackman, N);
X_kaiser_fft = fft(x1_kaiser, N);
X_flat_top_fft = fft(x1_flat_top, N);

% Define frequency bins


freq = (0:N-1)*(fs/N);

figure;

subplot(3,2,1);
stem(freq, abs(X_hanning_fft));
xlim([0 500]);

68
23102128 A6 Vansh Ahuja

title('Hanning Window FFT [23102128]');


xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

subplot(3,2,2);
stem(freq, abs(X_hamming_fft));
xlim([0 500]);
title('Hamming Window FFT [23102128]');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

subplot(3,2,3);
stem(freq, abs(X_blackman_fft));
xlim([0 500]);
title('Blackman Window FFT [23102128]');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

subplot(3,2,4);
stem(freq, abs(X_kaiser_fft));
xlim([0 500]);
title('Kaiser Window FFT [23102128]');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

subplot(3,2,5);
stem(freq, abs(X_flat_top_fft));
xlim([0 500]);
title('Flat-Top Window FFT [23102128]');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
grid on;

69
23102128 A6 Vansh Ahuja

Observations

1. Blackman and Flat-Top windows offer the best leakage suppression but at the cost of
resolution.
2. Hanning and Hamming provide a good trade-off between resolution and leakage.
3. Kaiser window allows customization using the shape parameter ( \beta ), making it
flexible.
4. Flat-Top is most suitable for accurate amplitude measurements, though it significantly
broadens the spectrum.

Learning Outcomes
1. Understanding Spectral Leakage: Signal truncation in the time domain spreads
frequency components, causing distortion.
2. Effect of Windowing: Rectangular windows increase leakage, while Hanning,
Hamming, and Blackman reduce it at the cost of frequency resolution.
3. Impact of Window Length: Longer windows improve resolution but may affect
precision.
4. FFT & Zero-Padding: Using a higher-point FFT (e.g., 1024) enhances frequency
resolution.
5. Practical Applications: Essential in communication systems, signal processing, and
VLSI design for leakage minimization.

70

You might also like