FFT plot in frequency domain, error help
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nina Perf
el 25 de Oct. de 2021
Comentada: Nina Perf
el 27 de Oct. de 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!
0 comentarios
Respuesta aceptada
Dave B
el 25 de Oct. de 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
Más respuestas (0)
Ver también
Categorías
Más información sobre Parametric Spectral Estimation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!