Invalid data type. First argument must be double, single, int8, uint8, int16, uint16, int32, uint32, or logical. Error

112 views (last 30 days)
So I am trying to understand fast fourier transformation on matlab but when I use Y=fft(y) comand I take the invalid data type error and I have no idea neither why I am taking that error or how do solve it?
syms t
A = 2;
w = 2*pi;
phi = 5;
T = (2*pi)/w % fundamental period
y = A*sin(w*t + phi)
fplot(y, [0 3]);
title('Simulation of harmonic function');
xlabel('t[s]');
ylabel('U[mV]');
%mean value
mean_value = (1/T)*int(y,t,0,T)
%power
syms T
temp = 1/(2*T)*int(y^2,-T,T);
power = limit(temp,T, inf)
%energy
temp = int(y^2,-T,T);
energy = limit(temp,T, inf)
%Fast Fourier Tranform
Y = fft(y)

Accepted Answer

Star Strider
Star Strider on 26 Mar 2021
Since ‘y’ is a symbolic expression, it is not an appropirate argument for fft.
However, all is not lost! First, get the ‘x’ and ‘y’ data from the fplot call:
hfp = fplot(y, [0 3]);
xv = hfp.XData;
yv = hfp.YData;
then call fft with ‘yv’:
Y = fft(yv);
and the code runs as expected.
  4 Comments
Onur Dikilitas
Onur Dikilitas on 26 Mar 2021
Okey clearly something is wrong because when I run the code with changes first I thought it works as expected. But when I add plot(Y) at the end I get this figure and it is not look like fft of sine signal.
Star Strider
Star Strider on 26 Mar 2021
The output of fft is a symmetrical complex vector.
Try this:
%Fast Fourier Tranform
Ts = 1/mean(diff(xv)); % Sampling Interval
Fs = 1/Ts; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
L = numel(xv); % Signal Length
Y = fft(yv)/L; % Fouriet Transform (Normalised)
Fv = linspace(0, 1, fix(L/2)+1)*Fn; % Frequency Vector
Iv = 1:numel(Fv); % Index Vector
figure
plot(Fv, abs(Y(Iv))*2)
grid
xlabel('Frequency')
ylabel('Amplitude')
xlim([0 1.5]*1E-3) % Optional
That should do what you want.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!