How to apply window function in Frequency domain
53 views (last 30 days)
Show older comments
I want to use window function but i got a problem
I want to apply window function like the picture below.
But if I want to apply hann window, it will be applied as shown in the picture below.
I wrote the code as below
window = hann(length(freq))'
X_filtered = Amplitude .* window;
The window function is always centered. I want to apply window function only between certain frequencies, is there a way?
Or is it possible to implement it in a bandpass filter or other way instead of window?
1 Comment
Mathieu NOE
on 28 Mar 2023
hello
you can create a odd sampled hanning window and make sure the center point lies at the correct target frequency
simply remove the left extra portion of window that is too long (would extend in the negative freq range)
Answers (1)
Balavignesh
on 16 Nov 2023
As per my understanding, you would like to apply the 'window' function to a specific frequency band of your data and filter out specific frequencies.
The window functions such as Hanning (Hann) are typically applied in the time domain before the Fourier Transform. They are used to mitigate the spectral leakage that occurs due to the finite length of the signal. If you're looking to filter out certain frequencies, a bandpass filter would be more suitable. You could use the 'fir1' function to design a bandpass filter, and use the 'filter' function to get the filtered signal.
The following example code may help you understand this:
% Define the sample rate and time vector
Fs = 1000; % Sample rate (Hz)
t = 0:1/Fs:1; % Time vector
% Create a sample signal that is a sum of two sinusoids. Input your own
% signal
f1 = 50; % Frequency of first sinusoid (Hz)
f2 = 200; % Frequency of second sinusoid (Hz)
x = sin(2*pi*f1*t) + sin(2*pi*f2*t);
% Define the cutoff frequencies and filter order for the bandpass filter
Fpass1 = 100; % First passband frequency (Hz)
Fpass2 = 300; % Second passband frequency (Hz)
N = 100; % Filter order
% Design a bandpass FIR filter
b = fir1(N, [Fpass1, Fpass2]/(Fs/2));
% Apply the filter to the sample signal
y = filter(b, 1, x);
% Compute the Fourier transforms of the original and filtered signals
X = fft(x);
Y = fft(y);
% Compute the frequency vector
f = (0:length(t)-1)*(Fs/length(t));
% Plot the magnitude spectra of the original and filtered signals
figure
subplot(2,1,1)
plot(f, abs(X))
title('Spectrum of Original Signal')
xlabel('Frequency (Hz)')
ylabel('Magnitude')
xlim([0 Fs/2]) % Only display frequencies up to the Nyquist frequency
subplot(2,1,2)
plot(f, abs(Y))
title('Spectrum of Filtered Signal')
xlabel('Frequency (Hz)')
ylabel('Magnitude')
xlim([0 Fs/2]) % Only display frequencies up to the Nyquist frequency
Kindly refer to the below documentation links to have more information on the following:
- 'filter' function: https://www.mathworks.com/help/matlab/ref/filter.html
- 'fir1' function: https://www.mathworks.com/help/signal/ref/fir1.html
- 'fft' function: https://www.mathworks.com/help/matlab/ref/fft.html
Hope that helps!
Balavignesh.
0 Comments
See Also
Categories
Find more on Filter Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!