2013-06-23 50 views
2

我試圖獲得某個波段的功率,但是我想在時域而不是頻域做到這一點。問題 - 樂隊非常緊張,因此使用簡單的過濾器會產生重疊的「尾巴」。時間卷積與頻率乘法

讓[a1 a2] Hz是我想要計算功率的頻段。我可以想象我將頻域乘以一個矩形信號,因此我可以及時得到它,所以我可以做一個時間卷積。

的代碼是:(MATLAB) 對於x - 在時間上的信號,X = FFT(x)中,W - 在頻率窗口中,w = IFFT(W)

filteredX=X.*W; 
Fx=ifft(filteredX); 
Fx2=conv(x,w,'same'); 

的結果信號是不同的。 雖然filteredX顯示正確的頻譜,但Fx2的fft(卷積結果)完全不同。

有什麼建議嗎?

編輯:

正如EitanT(感謝)的建議,我周圍用下面的代碼奏:

im = fix(255 * rand(500,1)); 
mask = ones(4,1)/16; 

% # Circular convolution 
resConv = conv(im, mask); 

% # Discrete Fourier transform 
M = size(im, 1) + size(mask, 1); 
resIFFT = ifft(fft(im, M) .* fft(mask, M)); 
% # Not needed any more - resIFFT = resIFFT(1:end-1); % # Adjust dimensions 

% # Check the difference 
max(abs(resConv(:) - resIFFT(:))) 

,工作正常,但我不能使用它,所以我不得不改變與尺寸問題有關的部分,並得到以下(請參閱評論):

im = fix(255 * rand(500,1)); 
mask = ones(4,1)/16; 

% # Circular convolution 
resConv = conv(im, mask,'same'); % # instead of conv(im, mask) 

% # Discrete Fourier transform 
M = size(im, 1) % # Instead of: M = size(im, 1) + size(mask, 1); 
resIFFT = ifft(fft(im, M) .* fft(mask, M)); 
resIFFT = resIFFT(1:end-1); % # Adjust dimensions 

% # Check the difference 
max(abs(resConv(:) - resIFFT(:))) 

如果雖然我希望得到s艾姆成績,現在的差距要高得多。

+0

可能重複(http://stackoverflow.com/questions/14025967/verify-the-convolution-theorem)。只需使用'fft'和'conv'而不是'fft2'和'conv2'。 –

+0

謝謝Eitan。使用引用的代碼玩了一下後,似乎問題與大小有關。我有一個1XN的頻率矩形(W)和一個1XN的頻譜(X)。 w的信號和信號x的長度分別是N個點和(N + N + 1)個長的卷積。在卷積中選擇「相同」選項會提取所需的N個點,但頻率結果與乘法不同。甚至不關閉 – BioSP

+0

默認情況下,'conv'函數正向過濾,所以「額外」時間點被添加到原始採樣的末尾。請參閱下面的答案以獲取更詳細的分類。 – cjh

回答

2

Eitan的早期verification of the convolution定理非常好。在這裏,我想演示用於濾波特定頻帶的時域卷積,並且顯示它相當於頻域乘法。

除了知道如何運行fftifft功能,也有在使這項工作所需的兩個部分:

  1. 確定由fft功能
  2. 填充返回的每個傅立葉分量的頻率(Hz)的fft之前的信號,以及ifft之後或conv之後的功能。

下面的代碼生成隨機信號,執行這兩個時域和頻率域濾波,以及示出了在圖像這樣的等價:

enter image description here

Nsamp = 50; %number of samples from signal X 
X = randn(Nsamp,1); %random Gaussian signal 
fs = 1000;  %sampling frequency 

NFFT = 2*Nsamp; %number of samples to be used in fft (this will lead to padding) 

bandlow = 250; %lower end of filter band (just an example range) 
bandhigh = 450; %upper end of filter band 

%construct a frequency axis so we know where Fourier components are 

if mod(NFFT,2) == 0 %if there is an even number of points in the FFT 
    iNyq = NFFT/2;  %index to the highest frequency 
    posfqs = fs*(0:iNyq)/NFFT; %positive frequencies 
    negfqs = -posfqs(end-1:-1:2); %negative frequencies 

else %if there is an odd number of point in the FFT 
    iNyq = floor(NFFT/2);  %index to the highest frequency 
    posfqs = fs*(0:iNyq)/NFFT; %positive frequencies 
    negfqs = -posfqs(end:-1:2); %negative frequencies 
end 

fqs = [posfqs'; negfqs']; %concatenate the positive and negative freqs 

fftX = fft(X,NFFT); % compute the NFFT-point discrete Fourier transform of X 
        % becuse NFFT > Nsamp, X is zero-padded 

% construct frequency-space mask for the desired frequency range 
W = ones(size(fftX)) .* (and (abs(fqs) >= bandlow, abs(fqs) < bandhigh)); 

fftX_filtered = fftX.*W; %multiplication in frequency space 
X_mult_filtered = ifft(fftX_filtered, NFFT); %convert the multiplicatively-filtered signal to time domain 

w = ifft(W,NFFT); %convert the filter to the time domain 
X_conv_filtered = conv(X, w); %convolve with filter in time domain 

%now plot and compare the frequency and time domain results 
figure; set(gcf, 'Units', 'normalized', 'Position', [0.45 0.15 0.4 0.7]) 

subplot(3,1,1); 
plot(X) 
xlabel('Time Samples'); ylabel('Values of X'); title('Original Signal') 

subplot(3,1,2); 
plot(X_conv_filtered(1:Nsamp)) 
xlabel('Time Samples'); ylabel('Values of Filtered X'); title('Filtered by Convolution in Time Domain') 

subplot(3,1,3); 
plot(X_mult_filtered(1:Nsamp)) 
xlabel('Time Samples'); ylabel('Values of Filtered X'); title('Filtered by Multiplication in Frequency Domain') 

for sp = 1:3; subplot(3,1,sp); axis([0 Nsamp -3 3]); end 
+0

非常感謝您的詳細解答!我應該花一點時間深入研究你寫的內容。但在此期間 - 謝謝! – BioSP

+0

@HelloWorld,不客氣。這是一個有趣的練習。另外需要注意的是,當在頻率空間中找到'W'時,重要的是包括正頻率和負頻率,以確保反FFT產生實值函數。 – cjh

-1

下面是一個極好的例子: https://www.youtube.com/watch?v=iUafo2UZowE

編輯:

只是不要忘記填充。一維信號是這樣的:如果你想繪製隨時間變化的響應

H=fft(h); 
X=fft(x); 

Y=H.*X; 

y=ifft(Y); 

stem(y) 

假設NH和NX對應的時間

lh=length(h); 
lx=length(x); 

h=[h zeros(1,lx-1)]; 
x=[x zeros(1,lh-1)]; 

其餘的是簡單h和x樣本,那麼響應樣本的相應時間可以這樣計算:

n=min(nh)+min(nx):+max(nh)+max(nx); 

最後的[驗證卷積定理]

stem(n,y)