Reputation:
I am trying to make a frequency spectrum up to 30 Hz of a Sine wave with period pi. I wrote a code but I keep getting the error : 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);
Upvotes: 0
Views: 1814
Reputation: 104484
The fft
is a numerical algorithm that accepts numeric inputs, not symbolic. I suspect that you are declaring t
before hand as symbolic. Simply move that t
assignment, as well as defining the sampling frequency Fs
, sampling time Ts
and the maximum time Tmax
at the beginning of your code and you're good to go. If I can suggest something, display the time domain in one graph, and the frequency domain in another. Use subplot
to help you do this. I would also recommend you plot the magnitude, as the FFT will generally give you complex-valued coefficients. The magnitude is what we generally look at with regards to frequency content in a signal.
One more suggestion I have is to change the way you are declaring your f
vector. I can't tell what the ending element is for f
in a general sense, and it looks inconsistent with the way t
is being defined. I've made a change to the f
vector to make it work. Because you are applying fftshift
, the beginning starts from -Fs/2
and the end is Fs/2
. Therefore:
%// 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
Upvotes: 2