I'm getting this error as shown below
Show older comments
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)
Walter Roberson
on 2 Aug 2022
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
Walter Roberson
on 2 Aug 2022
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.
Sean
on 2 Aug 2022
Walter Roberson
on 2 Aug 2022
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.
Categories
Find more on C2000 Microcontroller Blockset in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!