MATLAB Answers

problems with ifft of bandpass filtered white noise

13 views (last 30 days)
I need to bandpass filter the white noise signal of 2 sec lenght and my teacher wants me to use fft() to go from time domain to frequency domain, where I can just give value of 0 to frequencies of non interest. And then using ifft() to go back to time domain. The problem is: when I convert back to time domain the amplitude of the last 0.8 sec of the signal is near 0. Someone can look at my code and finde a problem, please?
signal efter ifft.PNG
%% Generate white noise
Fs = 40000; % Sampling frequency
F = 1000; % center frequency
t = 0:1/Fs:2; % Time vector
L = length(t); % Signal length
X=wgn(L,1,0); % White noise at 0 dB
figure(1)
plot(t,X)
title('White noise')
xlabel('Time (sec)')
ylabel('X(t)')
%% Cenvert from time domain to frequensy domain
n = 2^nextpow2(L);
Y = fft(X,n);
f = Fs*(0:n-1)/n;
P = abs(Y/n);
figure(2)
plot(f,P)
title('White noise in Frequency Domain')
xlabel('Frequency (Hz)')
ylabel('|P(f)|')
%% Bandpass filtering 1 ocatve centered at 1 kHz
N=length(Y);
bw_low=round(N*F/(Fs*sqrt(2)));
bw_high=round(N*F*sqrt(2)/Fs);
bw_low_s=round((Fs-2*F+F/sqrt(2))*N/Fs);
bw_high_s=round((Fs-2*F+F*sqrt(2))*N/Fs);
Y(1:bw_low-1)=0;
Y(bw_high+1:bw_low_s-1)=0;
Y(bw_high_s+1:end)=0;
P_BP = abs(Y/n);
figure(3)
plot(f,P_BP)
title('Bandpass White noise in Frequency Domain')
xlabel('Frequency (Hz)')
ylabel('|P(f)|')
%% Convert from frequency domain to time domain
X_1 = ifft(Y,L);
figure(4)
plot(t,X_1);
title('1 octave White noise centered at 1 kHz')
xlabel('Time (sec)')
ylabel('X(t)')
%%

  0 Comments

Sign in to comment.

Accepted Answer

Dimitris Kalogiros
Dimitris Kalogiros on 27 Sep 2019
Hi Anna.
Pay attention to these lines of your code:
n = 2^nextpow2(L);
Y = fft(X,n);
Because n>L, matlab adds some zeros at the end of signal X , in order to increase its length up to n. These are the zeros that you obzerve at the last figure;
I 'm giving you a "somewhat corrected" version of your code:
%% Generate white noise
Fs = 40000; % Sampling frequency
F = 1000; % center frequency
t = 0:1/Fs:2; % Time vector
L = length(t); % Signal length
X=wgn(L,1,0); % White noise at 0 dB
figure(1)
plot(t,X)
title('White noise')
xlabel('Time (sec)')
ylabel('X(t)')
%% Cenvert from time domain to frequensy domain
n = 2^nextpow2(L);
Y = fft(X,n);
f = Fs*(0:n-1)/n;
P = abs(Y/n);
figure(2)
plot(f,P)
title('White noise in Frequency Domain')
xlabel('Frequency (Hz)')
ylabel('|P(f)|')
%% Bandpass filtering 1 ocatve centered at 1 kHz
N=length(Y);
bw_low=round(N*F/(Fs*sqrt(2)));
bw_high=round(N*F*sqrt(2)/Fs);
bw_low_s=round((Fs-2*F+F/sqrt(2))*N/Fs);
bw_high_s=round((Fs-2*F+F*sqrt(2))*N/Fs);
Y(1:bw_low-1)=0;
Y(bw_high+1:bw_low_s-1)=0;
Y(bw_high_s+1:end)=0;
P_BP = abs(Y/n);
figure(3)
plot(f,P_BP)
title('Bandpass White noise in Frequency Domain')
xlabel('Frequency (Hz)')
ylabel('|P(f)|')
%% Convert from frequency domain to time domain
%X_1 = ifft(Y,L);
X_1 = ifft(Y,n);
X_1=X_1(1:length(X)); %remove n-L zeros.
figure(4)
%plot(t,X_1);
plot(t,real(X_1));
title('1 octave White noise centered at 1 kHz')
xlabel('Time (sec)')
ylabel('X(t)')
Tip: You used fft of length n, to trasform from time to frequency domain. You should use ifft of the same length (e.g. n ) to go back , from frequency to time domain.

  3 Comments

Anna Sergeeva
Anna Sergeeva on 27 Sep 2019
It is beautiful! Thank you så much :-) But I can not play the sound, get this error
error.PNG
Dimitris Kalogiros
Dimitris Kalogiros on 27 Sep 2019
Well... In my laptop, everything seems OK. I can hear X , using sound(X, Fs), and it is totaly a white noise signal and I can listen X_1 , using sound (real(X_1), Fs), and the differences from X are evident.
I think that the error you are getting has to do with your sound card and not with the matlab code.
Anna Sergeeva
Anna Sergeeva on 27 Sep 2019
It works now! There were some problems with my pc, restart helped:-) Thank you very much for your help!

Sign in to comment.

More Answers (0)

Sign in to answer this question.

Tags