MATLAB: Problems with ifft of bandpass filtered white noise

fft/ifft

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)')
%%

Best Answer

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.