fft error in function?
9 views (last 30 days)
Show older comments
Alia Hicks
on 28 Jul 2020
Commented: Walter Roberson
on 28 Jul 2020
data_output is a 60000x2 matrix.
Concerning line the line that is highlighted with arrows, "fhat = fft(f,n);", I keep recieving "Error using fft
Invalid data type. First argument must be double, single, int8, uint8, int16, uint16, int32,uint32, or logical.".
What can I do to make the code function? Are either t or f that is the problem?
dt = .001;
t = 0:dt:1;
f = 'data_output(:1)';
figure;
plot(t,ffilt); ",
dt = .001;
%dt = 0.00001666667;
t = 0:dt:1;
f = 'data_output(:1)';
figure;
plot(data_output)
xlabel('time(s)');
ylabel('Amplitude');
grid on
%% Compute the Fast Fourier Transform FFT
n = length(t);
----------------------------- >>>fhat = fft(f,n); <<< ------------------------
PSD = fhat.*conj(fhat)/n;
freq = 1/(dt*n)*(0:n);
L = 1:floor(n/2);
figure;
dim = 2;
plot(f,fhat,2); % FFT plot
xlim([0 1]);
xlabel('Frequency(Hz)');
ylabel('FFT Magnitude');
grid on
%% USe the PSD to filter out noise
indices PSD>35000;
PSDclean = PSD.*indices;
fhat = indices.*fhat;
ffilt = ifft(fhat);
figure;
plot(t,ffilt);
1 Comment
KSSV
on 28 Jul 2020
What for is this line?
f = 'data_output(:1)';
I guess it should be
f = data_output(:,1);
Isn't it? In your case what is
class(f)
Accepted Answer
Walter Roberson
on 28 Jul 2020
f = 'data_output(:1)';
That is a character vector. You cannot fft() a character vector.
If you need to apply fft to the ASCII codes that are used for the letters 'd' 'a' 't' and so on, then fft(double(f), n) . I would, however, suggest that it is very unlikely that is what you want, and that instead your line should be something closer to
f = data_output(:,1);
where data_output would be replaced by the name of the variable whose first column you want to extract.
plot(t,ffilt); ",
ffilt is not defined at that point.
You have a stray double-quote on that line.
plot(f,fhat,2); % FFT plot
When you use more than one numeric input to plot(), then you must have an even number of numeric inputs.
dim = 2;
That line hints that the 2 in your plot might refer to the second dimension, but your fhat looks like it will be a vector so it does not appear that giving the dimension would be useful.
If you are expecting that f or fhat are 2D arrays, then some of your other lines will have problems.
indices PSD>35000;
There is no MATLAB function named indices to invoke on the character vector 'PSD>35000' . I would suspect
indices = PSD>35000;
4 Comments
Walter Roberson
on 28 Jul 2020
What is size(data_output) ?
Is there a reason that you are making your number of samples for your time different than the number of data points you have? I would think it more likely that you should define
n = length(data_output);
dt = 0.001;
t = (0:n-1) * dt;
More Answers (0)
See Also
Categories
Find more on Fourier Analysis and Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!