0

I'm testing implementing a convolution by using an FFT way. Reference Link: Replicate MATLAB's `conv2()` in Frequency Domain

as you see, he uses fft2 & ifft2 and it works perfectly. But I want to use traditional FFT & IFFT for each col, row

My forward direction is ok. However, the inverse direction has problems that make my result go wrong. Can anyone know where I am doing something wrong? I put my re-write code here:

   function [ mO_1 ] = conv2_rewrite( mI, mH, convShape )
% CONV_SHAPE_FULL     = 1;
% CONV_SHAPE_SAME     = 2;
% CONV_SHAPE_VALID    = 3;

numRows = size(mI, 1); numCols = size(mI, 2);

numRowsKernel = size(mH, 1); numColsKernel = size(mH, 2);

switch(convShape) case('full') numRowsFft = numRows + numRowsKernel - 1; numColsFft = numCols + numColsKernel - 1; firstRowIdx = 1; firstColIdx = 1; lastRowIdx = numRowsFft; lastColdIdx = numColsFft; case('same') numRowsFft = numRows + numRowsKernel; numColsFft = numCols + numColsKernel; firstRowIdx = ceil((numRowsKernel + 1) / 2); firstColIdx = ceil((numColsKernel + 1) / 2); lastRowIdx = firstRowIdx + numRows - 1; lastColdIdx = firstColIdx + numCols - 1; case('valid') numRowsFft = numRows; numColsFft = numCols; firstRowIdx = numRowsKernel; firstColIdx = numColsKernel; lastRowIdx = numRowsFft; lastColdIdx = numColsFft; end fftsizeRow = 2^(nextpow2(numRowsFft)); fftsizeCol = 2^(nextpow2(numColsFft)); % test fft separable instead fft2 fft_data = fft(mI,fftsizeRow,1); fft_data1 = fft(fft_data,fftsizeCol,2);

fft_kernel = fft(mH,fftsizeRow,1); fft_kernel1 = fft(fft_kernel,fftsizeCol,2); % test ifft separable instead ifft2 % mO_fft2 = ifft2(fft2(mI, fftsizeRow, fftsizeCol) .* fft2(mH, fftsizeRow, fftsizeCol), 'symmetric'); % mO = ifft2(fft_data1 .* fft_kernel1, 'symmetric'); mO = ifft(fft_data1 .* fft_kernel1,fftsizeRow,1, 'symmetric'); mO = ifft(mO, fftsizeCol, 2, 'symmetric'); mO_1 = mO(firstRowIdx:lastRowIdx, firstColIdx:lastColdIdx); % mO_1_fft2 = mO_fft2(firstRowIdx:lastRowIdx, firstColIdx:lastColdIdx); end

Thanks!

Update figure (the data and output look like): input -> noiseLevel = conv2(input, kernel) -> input/noise + threshold enter image description here

enter image description here

It's hard to analyze why I'm wrong on some frames. Of course, there are many other factors in the processing but when I use fft2, and ifft2 there are no errors. I don't know what happens in this frame: enter image description here

  • Hi and welcome to DSP.SE. Can you please elaborate what do you mean when you say that "it goes wrong"? Do you have minimal example that you can add? – Irreducible Aug 09 '22 at 08:24
  • Please show your expected output and actual output, so we have some idea of what your (perceived?) problem is. Your code looks OK at first glance. – Cris Luengo Aug 09 '22 at 17:18
  • Sorry!! I can't share the data. But I can say my data is a radar signal, not an image, but I can treat them like an image. I use a convolution way to filter out the background noise. However, when I use my way, background noise is still there. – Hữu Minh Nguyễn Aug 10 '22 at 02:29

0 Answers0