why is ifft output not complex but double? (linear convolution implementation)

1 vue (au cours des 30 derniers jours)
Zhongze Chen
Zhongze Chen le 27 Fév 2019
I'm trying to implement my own linear convolution. I am converting both image array and filter array to their DFTs and taking IDFT of their dot product. but now I am getting the output g as type double. Both array are in the same size after I populating each of them with zeros at the tail so dot product can be taken. I also attach my final output g in the picture. Where is the problem?
clc;
clear all;
Img = imread('~/Desktop/stripes.jpg');
F = fftImg(Img); %fft of the image
h = mygaussian(4,44); %create a gaussian filter
H = arrangeFilter(h); %filter in 1*n arrangement
H = fft(H,55000); %filter being populates zeros to 55000 elements
g = ifft(F.*H); %getting inverse FFT of the image, which is the convolution
figure; %opens a MATLAB window for a figure
imagesc(g); %displays pwr
colormap(gray); %makes the display grayscale
axis image; %forces axes to match image dimensions
function f = fftImg(c) %arrange the image to an 1*n array
cgray = rgb2gray(c);
[rows,cols] = size(cgray);
z = zeros(1,rows*cols); %waited for being filled up
z = uint8(z);
count = 1; %tracking the index of z
for k=1:rows
for m = 1:cols
z(1,count) = cgray(k,m);
count = count + 1;
end
end
f = fft(z,55000);
end
function h = mygaussian(s,N)
[u,v] = meshgrid(-(N-1):N,(-(N-1):N)); %Creates arrays containing spatial coords.
h = exp(-(u.^2 + v.^2)/s); %Calculates a Gaussian function at all the coords.
end
function n = arrangeFilter(h) %arrange filter to an 1*n array
[rows,cols] = size(h);
n = zeros(1,rows*cols); %waited for being filled up
count1 = 1; %tracking the index of n
for k=1:rows
for m = 1:cols
n(1,count1) = h(k,m); %only gray image ranges from 0-255
count1 = count1 + 1;
end
end
fprintf('%i\n', n);
end
  3 commentaires
Zhongze Chen
Zhongze Chen le 27 Fév 2019
No, the problem is that g was already a double array before I apply g = real(g).
David Goodmanson
David Goodmanson le 3 Mar 2019
Hi Zhongze,
Isn't g supposed to be real (whidh you refer to as double)? Are you thinking that g should have some small imaginary numerical nuisance component that isn't there?

Connectez-vous pour commenter.

Réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by