# apply hanning window for signal processing

52 views (last 30 days)
asma on 15 Apr 2024
Answered: Star Strider on 15 Apr 2024
Hi,
I've heard adding a Hanning Window can help with data processing before you apply a Fourier Transform. With the code I have below, where can I add such a window?
Any help would be greatly appreciated.
% for the efficiency relative to an ideal grating of SHG at a phase mismatch
capital_lambda = 20e-6;
k2=8.66072; % second harmonic (k2=(2*pi/lambda_sh)*n(lambda_sh))
k1=17.65206; % fundemental frequency (k1=(2*pi/lambda_FF)*n(lambda_FF))
Delta_kk = k2-2*k1; % intrinsic wave-vector mismatch
sigma_l = 2; % sigma_l = l*sigma_avg
L = 10e-3; % Length;
l=capital_lambda/2; %
f(sigma_l)=exp(-(pi^2) * (sigma_l^2)/2*(l^2));
N=L/(capital_lambda/2); % number of domains
Delta_k=(Delta_kk-pi)/l;
E3= f(sigma_l) * (sinc((Delta_k * L)/2))^2 + ((1-f(sigma_l))/N);
%plot(N,E3)
xlabel('(Delta_k * L)/pi');
ylabel('SHG Effiency');

Star Strider on 15 Apr 2024
I do not understand your code, so I am not certain where to suggest adding the hann call in that context. (I specifically do not see an fft call.)
In general, add by multiplying it by the fft argument (that needs to be a column vector or a matrix of column vectors).
Example —
Fs = 250; % Sampl;ing Frequency
Fn = Fs/2; % Nyquist Frequency
L = 2; % Signal LEngth
t = linspace(0, Fs*L, Fs*L+1).'/Fs; % Time Vector (Column Vector)
s = sin(2*pi*t*[1:124]); % Signal Matrix
s = s + randn(size(s))/5; % Signal Matrix With Noise
figure
plot(t, s)
grid
xlabel('Time')
ylabel('Amplitude')
title('Signal in Time Domain')
N = size(s,1);
NFFT = 2^nextpow2(N); % For 'fft' Efficiency
FTsnw = fft(s, NFFT)/N; % Fourier Transform Of 's' — No Window
Fv = linspace(0, 1, NFFT/2+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
figure
plot(Fv, abs(FTsnw(Iv,:))*2)
grid
xlabel('Frequency')
ylabel('Magnitude')
title('Fourier Transform Of ''s'' — No Window')
xlim([0 Fn])
figure
plot(Fv, abs(FTsnw(Iv,:))*2)
grid
xlabel('Frequency')
ylabel('Magnitude')
title('Fourier Transform Of ''s'' — No Window')
xlim([0 10])
wv = hann(N); % 'Hann'Window Vector
FTshw = fft(s .* wv)/sum(wv); % Fourier Transform Of 's' — 'Hann' Window
figure
plot(Fv, abs(FTshw(Iv,:))*2)
grid
xlabel('Frequency')
ylabel('Magnitude')
title('Fourier Transform Of ''s'' — ''Hann'' Window')
xlim([0 Fn])
figure
plot(Fv, abs(FTshw(Iv,:))*2)
grid
xlabel('Frequency')
ylabel('Magnitude')
title('Fourier Transform Of ''s'' — ''Hann'' Window')
xlim([0 10])
This works essentially the same way with a signal vector.
NOTE — Tthe length of the window vector must be the same as the length of the original signal, not the ‘NFFT’
value.
.