How to apply window function in Frequency domain

조회 수: 55 (최근 30일)
영우
영우 2023년 3월 28일
답변: Balavignesh 2023년 11월 16일
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
Mathieu NOE
Mathieu NOE 2023년 3월 28일
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)

댓글을 달려면 로그인하십시오.

답변 (1개)

Balavignesh
Balavignesh 2023년 11월 16일
Hi 영우,
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:
Hope that helps!
Balavignesh.

카테고리

Help CenterFile Exchange에서 Filter Analysis에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by