FFT plot in frequency domain, error help

3 vues (au cours des 30 derniers jours)
Nina Perf
Nina Perf le 25 Oct 2021
Commenté : Nina Perf le 27 Oct 2021
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!

Réponse acceptée

Dave B
Dave B le 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)
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
  1 commentaire
Nina Perf
Nina Perf le 27 Oct 2021
Thanks! That worked

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Parametric Spectral Estimation dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by