FFT plot in frequency domain, error help
2 views (last 30 days)
Show older comments
Hi,
I did a myfft function in order to plot the (PSD,f) of a signal: 10696x1 double
[f, ~, ~, psd, ~] = myfft(Data, fs);
function [f, amplitude, phase, PSD, power] = myfft(signal, samplingRate)
if ~isempty(signal)
Fs = samplingRate;
T = 1/Fs;
L = length(signal);
t = (0:L-1)*T;
Y = fft(signal);
P2 = abs(Y);
P1 = P2(1:floor(L/2)+1,:);
P1(2:end-1,:) = 2*P1(2:end-1,:);
f = Fs*(0:(L/2))/L;
amplitude = P1;
phase = unwrap(angle(Y));
phase = phase(1:floor(L/2)+1,:);
% Power Spectrum
PSD = Y.*conj(Y);
PSD = (1/(Fs*L)) .* PSD(1:floor(L/2)+1,:);
PSD(2:end-1,:) = 2*PSD(2:end-1,:);
power = sum(PSD);
else
f = [];
amplitude = [];
phase = [];
power = NaN;
PSD = [];
end
I get this error:
% Error using *
% Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the
% number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
% Error in myfft (line 10)
% t = (0:L-1)*T; % signal duration (time vector)
% Error in s (line 19)
% [f, ~, ~, psd, ~] = myfft(Data, fs); ;
Can you please help?
Thank you in advance!
0 Comments
Accepted Answer
Dave B
on 25 Oct 2021
What is size(Data) and size(fs)? Your function doesn't error for me when I give it a scalar fs and a vector signal:
load handel.mat
size(y)
size(Fs)
[f, ~, ~, psd, ~] = myfft(y, Fs);
plot(f,psd)
function [f, amplitude, phase, PSD, power] = myfft(signal, samplingRate)
if ~isempty(signal)
Fs = samplingRate;
T = 1/Fs;
L = length(signal);
t = (0:L-1)*T;
Y = fft(signal);
P2 = abs(Y);
P1 = P2(1:floor(L/2)+1,:);
P1(2:end-1,:) = 2*P1(2:end-1,:);
f = Fs*(0:(L/2))/L;
amplitude = P1;
phase = unwrap(angle(Y));
phase = phase(1:floor(L/2)+1,:);
% Power Spectrum
PSD = Y.*conj(Y);
PSD = (1/(Fs*L)) .* PSD(1:floor(L/2)+1,:);
PSD(2:end-1,:) = 2*PSD(2:end-1,:);
power = sum(PSD);
else
f = [];
amplitude = [];
phase = [];
power = NaN;
PSD = [];
end
end
More Answers (0)
See Also
Categories
Find more on Parametric Spectral Estimation 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!