FFT plot in frequency domain, error help

2 次查看(过去 30 天)
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!

采纳的回答

Dave B
Dave B 2021-10-25
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)
ans = 1×2
73113 1
size(Fs)
ans = 1×2
1 1
[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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Parametric Spectral Estimation 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by