FFT plot in frequency domain, error help

조회 수: 2 (최근 30일)
Nina Perf
Nina Perf 2021년 10월 25일
댓글: Nina Perf 2021년 10월 27일
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