0% found this document useful (0 votes)
16 views18 pages

STFT Analysis of Cosine and Chirp Signals

This experiment investigates the application of Short-Time Fourier Transform (STFT) to analyze the frequency content of a stationary cosine signal and a non-stationary chirp signal using different window functions and overlap conditions. The results demonstrate that while rectangular windows without overlap capture stationary frequencies, Hann and Hamming windows with overlap improve frequency resolution for non-stationary signals, revealing the trade-offs between time and frequency resolution. The study highlights the importance of windowing techniques in achieving optimal time-frequency resolution in signal analysis.

Uploaded by

nikkiant16
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)
16 views18 pages

STFT Analysis of Cosine and Chirp Signals

This experiment investigates the application of Short-Time Fourier Transform (STFT) to analyze the frequency content of a stationary cosine signal and a non-stationary chirp signal using different window functions and overlap conditions. The results demonstrate that while rectangular windows without overlap capture stationary frequencies, Hann and Hamming windows with overlap improve frequency resolution for non-stationary signals, revealing the trade-offs between time and frequency resolution. The study highlights the importance of windowing techniques in achieving optimal time-frequency resolution in signal analysis.

Uploaded by

nikkiant16
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

Experiment 2

Abstract
This experiment inves gates the applica on of Short-Time Fourier Transform (STFT) to analyze the
frequency content of two signals: a simple cosine signal f(t)=cos(2π⋅50t) and a chirp signal
f(t)=cos(2π(10t+2t^2)). The signals are sampled at different frequencies, and the STFT is performed
using rectangular, Hann, and Hamming windows with and without overlap. Magnitude spectrograms
are generated to visualize the me-frequency characteris cs of each signal, revealing how the STFT
captures both sta onary and non-sta onary frequency components.

Introduc on
In signal processing, the Fourier Transform is commonly used to convert a me-domain signal into its
frequency-domain representa on. However, it is not suitable for analyzing signals whose frequency
content changes over me. The Short-Time Fourier Transform (STFT) resolves this limita on by
dividing the signal into small overlapping or non-overlapping segments and performing a Fourier
Transform on each segment, providing me-localized frequency informa on.

This experiment applies the STFT on two dis nct signals—a sta onary sinusoidal signal and a non-
sta onary chirp signal—using different window func ons (Rectangular, Hann, and Hamming) and
varying overlap condi ons. The goal is to visualize the frequency varia ons over me and compare
the effects of different windowing techniques on the me-frequency resolu on of the STFT.

Windowing func ons are applied to segments of a signal before performing a Fourier Transform to
reduce spectral leakage and improve frequency resolu on.

Rectangular window

The Rectangular window is the simplest form, where the signal is unaltered within the window's
dura on but suffers from significant spectral leakage, resul ng in poor frequency resolu on.

Hann window and Hamming window

The Hann window and Hamming window are smoother, tapered window func ons that reduce
leakage more effec vely than the rectangular window. The Hann window is fully sinusoidal, while the
Hamming window has a slight adjustment to its shape, reducing leakage even further but at the cost
of a slight increase in the width of the main lobe in the frequency domain.

Discrete Fourier Transform (DFT)

The Discrete Fourier Transform (DFT) is used to convert these windowed signal segments from the
me domain to the frequency domain. It helps analyze the frequency components within each
segment, producing me-localized frequency informa on. When used in conjunc on with window
func ons in the Short-Time Fourier Transform (STFT), the DFT enables the visualiza on of how signal
frequencies evolve over me, especially in non-sta onary signals like chirps.
Problem Statement
The objec ve of this experiment is to perform STFT on the following signals:

1. f(t)=cos(2π⋅50t) sampled at 250 Hz.

2. f(t)=cos(2π(10t+2t^2)) over a me range of 0 to 25 seconds, with an appropriately selected


sampling frequency.

Specifically, the task involves:

1. Applying a rectangular window with no overlap.

2. Applying Hann and Hamming windows with 30% overlap.

3. Plo ng the magnitude of STFT as a func on of me (sec) and frequency (Hz).

4. Inves ga ng the effect of windowing and overlap on the me-frequency resolu on.

Methodology
This experiment involves analyzing two signals: a sta onary cosine signal f(t)=cos(2π⋅50t) and a non-
sta onary chirp signal f(t)=cos(2π(10t+2t^2)), using the Short-Time Fourier Transform (STFT) with
different window func ons and overlap condi ons.

1) Signal Genera on and Sampling

 Sta onary Signal: The signal f(t)=cos(2π⋅50t) is generated over a me range of 0 to 2.49
seconds and sampled at a frequency of 250 Hz to create a discrete- me version. The discrete
signal is segmented into windows of 0.2 seconds (50 samples) using a rectangular window.

 Non-Sta onary Chirp Signal: The chirp signal f(t)=cos(2π(10t+2t^2)) is generated over 25
seconds. It has a me-varying frequency that increases from 10 Hz upwards. This signal is
sampled at a suitable frequency (we are considering 250 Hz), and windowing is applied
similarly with a rectangular, Hann, or Hamming window.

2) Windowing of Signals

 For both signals, the rectangular window is applied first. Addi onally, Hann and Hamming
windows are used with 30% overlap for be er frequency resolu on. The window length
remains 0.2 seconds (adjusted based on the sampling frequency), and the signals are divided
into equal-sized windows.

3) STFT Computa on

a) Extrac ng a window from the signal: For each me segment (window), the signal por on
corresponding to that window is extracted.

b) Windowing the signal: The extracted segment is mul plied by the respec ve window func on
(rectangular, Hann, or Hamming).

c) Performing DFT: The Discrete Fourier Transform (DFT) is computed using the Fast Fourier
Transform (FFT) for each windowed segment.
d) Storing magnitudes in STFT matrix: The magnitude of the resul ng frequency components from
the DFT is stored in an STFT matrix for further visualiza on.

4) Plo ng STFT Results

 The STFT results are visualized as me-frequency spectrograms. The x-axis represents me,
the y-axis represents frequency, and the color intensity indicates the magnitude of the
frequency components. This allows the observa on of sta onary frequency content for the
cosine signal and the frequency sweep for the chirp signal.

Resul ng Plots:
First signal
Second signal:
Results
1. Sta onary Signal f(t)=cos(2π⋅50t):

o The magnitude spectrum for the rectangular window without overlap clearly shows
a sta onary frequency at 50 Hz.

o Hann and Hamming windows with 30% overlap smoothen the frequency resolu on,
reducing spectral leakage.

2. Non-Sta onary Chirp Signal f(t)=cos(2π(10t+2t^2)):

o The rectangular window without overlap captures the non-sta onary frequency
content, showing increasing frequency over me.

o Hann and Hamming windows with overlap provide be er resolu on, but the
tradeoff between me and frequency resolu on becomes apparent.

Observa ons
 The rectangular window without overlap results in no ceable frequency leakage and
reduced resolu on in both me and frequency domains.

 The Hann and Hamming windows, while offering be er spectral resolu on, introduce trade-
offs in terms of localiza on in me.

 The chirp signal exhibits a smooth frequency sweep, and the effect of windowing is evident
in how the STFT captures this progression.

Discussion
The STFT effec vely provides insight into the me-varying frequency content of signals. The choice of
window func on and overlap has a significant impact on the trade-off between me and frequency
resolu on. In the case of a sta onary signal f(t)=cos(2π⋅50t), the windowing techniques only improve
spectral leakage but do not reveal any new me-varying content. However, for the chirp signal, which
has me-varying frequency content, the STFT successfully captures the frequency progression over
me, with the Hann and Hamming windows offering smoother transi ons.

Conclusion
This experiment demonstrated the prac cal applica on of the Short-Time Fourier Transform (STFT)
for analyzing both sta onary and non-sta onary signals. The choice of windowing func on and
overlap plays a crucial role in determining the me-frequency trade-off. For sta onary signals, any
windowing func on will adequately capture the constant frequency, while non-sta onary signals
benefit more from smoother window func ons like Hann or Hamming. This study highlights the
importance of understanding the characteris cs of window func ons to achieve op mal me-
frequency resolu on in STFT.
MATLAB Code:
Signal -> f(t)=cos(2π⋅50t)

Rectangular
1. >> clc;
2. clear all;
3. close all;
4.
5. t = 0:0.01:2.49;
6. x = cos(2*pi*50*t); % continuous signal
7. plot(t, x);
8. xlabel('t (sec)');
9. ylabel('f(t)');
10. title('Continuous-Time Signal');
11. figure;
12.
13. fs = 250; % Sampling frequency
14. Ts = 1/fs; % Sampling period
15. n = 0:Ts:0.996;
16. a1 = cos(2*pi*50*n); % discrete signal
17. stem(n, a1);
18. xlabel('n (sec)');
19. ylabel('F(n)');
20. title('Discrete-Time Signal');
21. figure;
22.
23. window_length_in_time = 0.2; % Window length in sec
24. window_length_in_samples = 0.2 * fs; % window length in samples
25. number_of_windows = floor(length(a1) / window_length_in_samples); % Number
of windows
26.
27. w = ones(window_length_in_samples, 1); % Rectangular window
28. stem(w);
29. xlabel('n');
30. ylabel('W(n)');
31. title('Rectangular Window');
32. figure;
33.
34. % STFT Computation
35. step_size = window_length_in_samples; % No overlap
36. dft_points = window_length_in_samples; % Number of DFT points
37. stft = zeros(window_length_in_samples, number_of_windows); % Initialize
STFT matrix
38.
39. for i = 1:number_of_windows
40.
41. signal_window = a1((i-1)*window_length_in_samples+1 :
i*window_length_in_samples); % Extract window from signal
42. product = (signal_window'.*w) ; % multiplication
43. dft = fft(product, dft_points); % Performing DFT
44. stft(:, i) = abs(dft); % Storing the magnitude of DFT in STFT matrix
45.
46. end
47.
48. % Time and frequency axes
49. time_axis = (0:number_of_windows-1) * (window_length_in_time); % Time (in
seconds)
50. freq_axis = (0:dft_points-1) * fs / dft_points; % Frequency
(in Hz)
51.
52. % Plot the magnitude of STFT
53.
54. imagesc(time_axis, freq_axis, stft);
55. axis xy;
56. xlabel('Time (sec)');
57. ylabel('Frequency (Hz)');
58. title('Magnitude of STFT (Rectangular Window-No Overlap)');
59. colorbar;

Hamming

1. >> clc;
2. clear all;
3. close all;
4.
5. t = 0:0.01:2.49;
6. x = cos(2*pi*50*t); % continuous signal
7. plot(t, x);
8. xlabel('t (sec)');
9. ylabel('f(t)');
10. title('Continuous-Time Signal');
11. figure;
12. fs = 250; % Sampling frequency
13. Ts = 1/fs; % Sampling period
14. n = 0:Ts:5.796;
15. a1 = cos(2*pi*50*n); % discrete signal
16. stem(n, a1);
17. xlabel('n (sec)');
18. ylabel('F(n)');
19. title('Discrete-Time Signal');
20. figure;
21.
22.
23. % Zero Padding
24. zeroes = 15; % Number of zeros to pad at the beginning and end
25. a2= [zeros(1, zeroes), a1, zeros(1, zeroes)]; % Pad with zeros
26.
27. window_length_in_time = 0.2; % Window length in sec
28. window_length_in_samples = 0.2 * fs; % window length in samples
29.
30. M = window_length_in_samples;
31. w = 0.54 - 0.46 * cos(2*pi*(0:M-1)'/(M-1)); % Hamming window
32. stem(w);
33. xlabel('n');
34. ylabel('W(n)');
35. title('Hamming Window');
36. figure;
37.
38. overlap = round(0.3 * window_length_in_samples); % 30% overlap
39. step_size = window_length_in_samples - overlap; % Step size for sliding
window
40. dft_points = window_length_in_samples; % Number of DFT points
41.
42.
43. % STFT calculation
44. number_of_windows = floor((length(a2) - overlap) / step_size) + 1; % Number
of windows
45. stft = zeros(window_length_in_samples, number_of_windows); % Initialize
STFT matrix
46.
47. % Extract window from signal
48. for i = 1:number_of_windows
49. start_index = (i - 1) * step_size + 1;
50. end_index = start_index + window_length_in_samples - 1;
51.
52. if end_index > length(a2)
53. end_index = length(a2);
54. signal_window = [a2(start_index:end_index), zeros(1,
window_length_in_samples - (end_index - start_index + 1))];
55. else
56. signal_window = a2(start_index:end_index);
57. end
58. product = signal_window' .* w; % multiplication
59. dft = fft(product, dft_points); % Performing DFT
60. stft(:, i) = abs(dft); % Storing the magnitude of DFT in STFT matrix
61. end
62.
63. % Time and frequency axes
64. time_axis = (0:number_of_windows-1) * (step_size/fs) - 2; % Time (in
seconds)
65. freq_axis = (0:window_length_in_samples-1) * (fs/window_length_in_samples);
% Frequency (in Hz)
66.
67. % Plotting the STFT
68. figure;
69. imagesc(time_axis, freq_axis, abs(stft));
70. axis xy;
71. xlabel('Time (sec)');
72. ylabel('Frequency (Hz)');
73. title('Magnitude of STFT (Hamming Window - 30% Overlap)');
74. colorbar;

Hann

1. >> clc;
2. clear all;
3. close all;
4.
5. t = 0:0.01:2.49;
6. x = cos(2*pi*50*t); % continuous signal
7. plot(t, x);
8. xlabel('t (sec)');
9. ylabel('f(t)');
10. title('Continuous-Time Signal');
11. figure;
12. fs = 250; % Sampling frequency
13. Ts = 1/fs; % Sampling period
14. n = 0:Ts:5.796;
15. a1 = cos(2*pi*50*n); % discrete signal
16. stem(n, a1);
17. xlabel('n (sec)');
18. ylabel('F(n)');
19. title('Discrete-Time Signal');
20. figure;
21.
22.
23. % Zero Padding
24. zeroes = 15; % Number of zeros to pad at the beginning and end
25. a2= [zeros(1, zeroes), a1, zeros(1, zeroes)]; % Pad with zeros
26.
27. window_length_in_time = 0.2; % Window length in sec
28. window_length_in_samples = 0.2 * fs; % window length in samples
29.
30. M = window_length_in_samples;
31. w = 0.5 * (1 - cos(2*pi*(0:M-1)'/(M-1))); % Hann window
32. stem(w);
33. xlabel('n');
34. ylabel('W(n)');
35. title('Hann Window');
36. figure;
37.
38. overlap = round(0.3 * window_length_in_samples); % 30% overlap
39. step_size = window_length_in_samples - overlap; % Step size for sliding
window
40. dft_points = window_length_in_samples; % Number of DFT points
41.
42.
43. % STFT calculation
44. number_of_windows = floor((length(a2) - overlap) / step_size) + 1; % Number
of windows
45. stft = zeros(window_length_in_samples, number_of_windows); % Initialize
STFT matrix
46.
47. % Extract window from signal
48. for i = 1:number_of_windows
49. start_index = (i - 1) * step_size + 1;
50. end_index = start_index + window_length_in_samples - 1;
51.
52. if end_index > length(a2)
53. end_index = length(a2);
54. signal_window = [a2(start_index:end_index), zeros(1,
window_length_in_samples - (end_index - start_index + 1))];
55. else
56. signal_window = a2(start_index:end_index);
57. end
58. product = signal_window' .* w; % multiplication
59. dft = fft(product, dft_points); % Performing DFT
60. stft(:, i) = abs(dft); % Storing the magnitude of DFT in STFT matrix
61. end
62.
63. % Time and frequency axes
64. time_axis = (0:number_of_windows-1) * (step_size/fs) - 2; % Time (in
seconds)
65. freq_axis = (0:window_length_in_samples-1) *
(fs/window_length_in_samples);% Frequency (in Hz)
66.
67. % Plotting the STFT
68. figure;
69. imagesc(time_axis, freq_axis, abs(stft));
70. axis xy;
71. xlabel('Time (sec)');
72. ylabel('Frequency (Hz)');
73. title('Magnitude of STFT (Hann Window - 30% Overlap)');
74. colorbar;

Signal -> f(t)=cos(2π(10t+2t^2))

Rectangular

1. >> clc;
2. clear all;
3. close all;
4.
5. fo=10;
6. k=2;
7. t=0:0.1:25;
8. x=cos([Link].(fo.*t+k.*t.^2)); % chirp signal
9. plot(t,x);
10. xlabel('x');
11. title('continuous signal');
12. figure;
13.
14. fs=250; % ( min fs >= 220 hz, therefore we can consider 250 hz )
15. Ts=1/250;
16. n=0:Ts:25;
17.
18.
19. a1=cos(2*pi*(fo*n+k*n.^2)); % discrete signal
20. stem(n,a1);
21. title('discrete signal');
22. figure;
23.
24. window_length_in_time = 0.2; % Window length in sec
25. window_length_in_samples = 0.2 * fs; % window length in samples
26. number_of_windows = floor(length(a1) / window_length_in_samples); % Number
of windows
27.
28. w = ones(window_length_in_samples, 1); % Rectangular window
29. stem(w);
30. xlabel('n');
31. ylabel('W(n)');
32. title('Rectangular Window');
33. figure;
34.
35. % STFT Computation
36. step_size = window_length_in_samples; % No overlap
37. dft_points = window_length_in_samples; % Number of DFT points
38. stft = zeros(window_length_in_samples, number_of_windows); % Initialize
STFT matrix
39.
40. for i = 1:number_of_windows
41.
42. signal_window = a1((i-1)*window_length_in_samples+1 :
i*window_length_in_samples); % Extract window from signal
43. product = (signal_window'.*w) ; % multiplication
44. dft = fft(product, dft_points); % Performing DFT
45. stft(:, i) = abs(dft); % Storing the magnitude of DFT in STFT matrix
46.
47. end
48.
49. % Time and frequency axes
50. time_axis = (0:number_of_windows-1) * (window_length_in_time); % Time (in
seconds)
51. freq_axis = (0:dft_points-1) * fs / dft_points; % Frequency
(in Hz)
52.
53. % Plot the magnitude of STFT
54.
55. imagesc(time_axis, freq_axis, stft);
56. axis xy;
57. xlabel('Time (sec)');
58. ylabel('Frequency (Hz)');
59. title('Magnitude of STFT (Rectangular Window-No Overlap)');
colorbar;

Hamming

1. >> clc;
2. clear all;
3. close all;
4.
5. fo=10;
6. k=2;
7. t=0:0.1:25;
8. x=cos(2.*pi.*(fo.*t+k.*t.^2)); % chirp signal
9. plot(t,x);
10. xlabel('x');
11. title('continuous signal');
12. figure;
13.
14. fs=250; % ( min fs >= 220 hz, therefore we can consider 250 hz )
15. Ts=1/250;
16. n=0:Ts:25;
17.
18.
19. a1=cos(2*pi*(fo*n+k*n.^2)); % discrete signal
20. stem(n,a1);
21. title('discrete signal');
22. figure;
23.
24. % Zero Padding
25. zeroes = 15; % Number of zeros to pad at the beginning and end
26. a2= [zeros(1, zeroes), a1, zeros(1, zeroes)]; % Pad with zeros
27.
28. window_length_in_time = 0.2; % Window length in sec
29. window_length_in_samples = 0.2 * fs; % window length in samples
30.
31. M = window_length_in_samples;
32. w = 0.54 - 0.46 * cos(2*pi*(0:M-1)'/(M-1)); % Hamming window
33. stem(w);
34. xlabel('n');
35. ylabel('W(n)');
36. title('Hamming Window');
37. figure;
38.
39. overlap = round(0.3 * window_length_in_samples); % 30% overlap
40. step_size = window_length_in_samples - overlap; % Step size for sliding
window
41. dft_points = window_length_in_samples; % Number of DFT points
42.
43.
44. % STFT calculation
45. number_of_windows = floor((length(a2) - overlap) / step_size) + 1; % Number
of windows
46. stft = zeros(window_length_in_samples, number_of_windows); % Initialize
STFT matrix
47.
48. % Extract window from signal
49. for i = 1:number_of_windows
50. start_index = (i - 1) * step_size + 1;
51. end_index = start_index + window_length_in_samples - 1;
52.
53. if end_index > length(a2)
54. end_index = length(a2);
55. signal_window = [a2(start_index:end_index), zeros(1,
window_length_in_samples - (end_index - start_index + 1))];
56. else
57. signal_window = a2(start_index:end_index);
58. end
59. product = signal_window' .* w; % multiplication
60. dft = fft(product, dft_points); % Performing DFT
61. stft(:, i) = abs(dft); % Storing the magnitude of DFT in STFT matrix
62. end
63.
64. % Time and frequency axes
65. time_axis = (0:number_of_windows-1) * (step_size/fs) - 2; % Time (in
seconds)
66. freq_axis = (0:window_length_in_samples-1) * (fs/window_length_in_samples);
% Frequency (in Hz)
67.
68. % Plotting the STFT
69. figure;
70. imagesc(time_axis, freq_axis, abs(stft));
71. axis xy;
72. xlabel('Time (sec)');
73. ylabel('Frequency (Hz)');
74. title('Magnitude of STFT (Hamming Window - 30% Overlap)');
75. colorbar;

Hann:

1. >> clc;
2. clear all;
3. close all;
4.
5. fo=10;
6. k=2;
7. t=0:0.1:25;
8. x=cos(2.*pi.*(fo.*t+k.*t.^2)); % chirp signal
9. plot(t,x);
10. xlabel('x');
11. title('continuous signal');
12. figure;
13.
14. fs=250; % ( min fs >= 220 hz, therefore we can consider 250 hz )
15. Ts=1/250;
16. n=0:Ts:25;
17.
18.
19. a1=cos(2*pi*(fo*n+k*n.^2));
20. stem(n,a1);
21. title('discrete signal');
22. figure;
23. % Zero Padding
24. zeroes = 15; % Number of zeros to pad at the beginning and
end
25. a2= [zeros(1, zeroes), a1, zeros(1, zeroes)]; % Pad with zeros
26.
27. window_length_in_time = 0.2; % Window length in sec
28. window_length_in_samples = 0.2 * fs; % window length in samples
29. M = window_length_in_samples;
30. w = 0.5 * (1 - cos(2*pi*(0:M-1)'/(M-1))); % Hann window
31. stem(w);
32. xlabel('n');
33. ylabel('W(n)');
34. title('Hann Window');
35. figure;
36. overlap = round(0.3 * window_length_in_samples); % 30% overlap
37. step_size = window_length_in_samples - overlap; % Step size for sliding
window
38. dft_points = window_length_in_samples; % Number of DFT points
39. % STFT calculation
40. number_of_windows = floor((length(a2) - overlap) / step_size) + 1; % Number
of windows
41. stft = zeros(window_length_in_samples, number_of_windows); % Initialize
STFT matrix
42. % Extract window from signal
43. for i = 1:number_of_windows
44. start_index = (i - 1) * step_size + 1;
45. end_index = start_index + window_length_in_samples - 1;
46.
47. if end_index > length(a2)
48. end_index = length(a2);
49. signal_window = [a2(start_index:end_index), zeros(1,
window_length_in_samples - (end_index - start_index + 1))];
50. else
51. signal_window = a2(start_index:end_index);
52. end
53. product = signal_window' .* w; % multiplication
54. dft = fft(product, dft_points); % Performing DFT
55. stft(:, i) = abs(dft); % Storing the magnitude of DFT in STFT matrix
56. end
57. % Time and frequency axes
58. time_axis = (0:number_of_windows-1) * (step_size/fs) - 2; % Time (in
seconds)
59. freq_axis = (0:window_length_in_samples-1) *
(fs/window_length_in_samples);% Frequency (in Hz)
60. % Plotting the STFT
61. figure;
62. imagesc(time_axis, freq_axis, abs(stft));
63. axis xy;
64. xlabel('Time (sec)');
65. ylabel('Frequency (Hz)');
66. title('Magnitude of STFT (Hann Window - 30% Overlap)');
67. colorbar;

You might also like