I'm getting this error as shown below

The code
function [Xk] = DFT(xn)
N = length(xn); %frame size
Xk = zeros(1,N); %array size xn
%DFT Algorithom
for k = 0:(N-1)
n = 0:(N-1);
Xk(k+1) = sum(xn(n+1).*exp((-1i*2*pi*k*n)/N));
end
Xk = abs(Xk);
end
The error
The left-hand side has been constrained to be non-complex, but the right-hand side is complex. To correct this problem, make the right-hand side real using the function REAL, or change the initial assignment to the left-hand side variable to be a complex value using the COMPLEX function.
Please Help

Answers (1)

function [Xkr] = DFT(xn)
N = length(xn); %frame size
Xk = complex(zeros(1,N)); %array size xn
Xkr = zeros(1,N);
%DFT Algorithom
for k = 0:(N-1)
n = 0:(N-1);
Xk(k+1) = sum(xn(n+1).*exp((-1i*2*pi*k*n)/N));
end
Xkr = abs(Xk);
end

3 Comments

I am not sure why you are taking abs(): discrete fourier transform of real data always returns complex values unless the phases are all 0.
cause I thought it might fix the error since my testbench doesnt have imaginary values
I think you should code
function [Xk] = DFT(xn)
N = length(xn); %frame size
Xk = complex(zeros(1,N)); %array size xn
n = 0:(N-1);
%DFT Algorithom
for k = 0:(N-1)
Xk(k+1) = sum(xn(n+1).*exp((-1i*2*pi*k*n)/N));
end
end
but that each place you call this function, you should code as expecting a complex result. Which you can take the abs() or real() or imag() part of, as appropriate.

Sign in to comment.

Products

Release

R2022a

Asked:

on 2 Aug 2022

Commented:

on 2 Aug 2022

Community Treasure Hunt

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

Start Hunting!