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 个)
Walter Roberson
2022-8-2
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 个评论
Walter Roberson
2022-8-2
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
2022-8-2
Walter Roberson
2022-8-2
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 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!