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

回答(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 个评论

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.

请先登录,再进行评论。

类别

在 帮助中心 和 File Exchange 中查找有关 C2000 Microcontroller Blockset 的更多信息

产品

版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by