MATLAB 'sym' 类型输入参数的未定义函数 'fft'

Undefined function 'fft' for input arguments of type 'sym' MATLAB

我正在尝试制作频率为 pi 的正弦波的高达 30 Hz 的频谱。我写了一段代码,但我不断收到错误消息:Undefined function 'fft' for input arguments of type 'sym'

sint = sin(t);
Tmax = 2*pi;  %the end sample value
Ts = 0.1;   %the sampling rate
N = (Tmax/Ts)+1;  %The number of samples
Fs = 1/Ts;
t = 0:Ts:Tmax;
plot(t,sint);
f = 0:fs/(N-1):30;
z = fftshift(fft(sint));
plot(f,z);

fft 是一种数值 算法,它接受数值输入,而不是符号输入。我怀疑您事先声明 t 是象征性的。只需移动 t 赋值,并在代码开头定义采样频率 Fs、采样时间 Ts 和最长时间 Tmax 即可去。如果我能提出建议,请在一张图中显示时域,在另一张图中显示频域。使用 subplot 来帮助您做到这一点。我还建议您绘制 magnitude,因为 FFT 通常会给您复值系数。幅值是我们通常所关注的信号中的频率成分。

我还有一个建议是更改您声明 f 向量的方式。我无法从一般意义上说出 f 的结束元素是什么,而且它看起来与 t 的定义方式不一致。我对 f 向量进行了更改以使其工作。因为你申请的是fftshift,所以开头是-Fs/2,结尾是Fs/2。因此:

%// Move here
Tmax = 2*pi;  %the end sample value
Ts = 0.1;   %the sampling rate
Fs = 1/Ts;
t = 0:Ts:Tmax;

sint = sin(t);
N = (Tmax/Ts)+1;  %The number of samples
figure; subplot(2,1,1); %// Change
plot(t,sint);
%f = 0:fs/(N-1):30; %// Change here
f = linspace(-Fs/2, Fs/2, numel(t)+1);
f(end) = [];   
z = fftshift(fft(sint));
subplot(2,1,2); %// Change
plot(f,abs(z)); %// Change