problems with ifft of bandpass filtered white noise

조회 수: 7 (최근 30일)
Anna Sergeeva
Anna Sergeeva 2019년 9월 25일
댓글: Anna Sergeeva 2019년 9월 27일
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)')
%%

채택된 답변

Dimitris Kalogiros
Dimitris Kalogiros 2019년 9월 27일
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
Dimitris Kalogiros
Dimitris Kalogiros 2019년 9월 27일
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 2019년 9월 27일
It works now! There were some problems with my pc, restart helped:-) Thank you very much for your help!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Measurements and Spatial Audio에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by