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

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:

