My Fourier Series plotter isn't outputting correct y-values

1 view (last 30 days)
This code outputs the power spectrum of the calculated Fourier Series. I'd like to output the y values of the Fourier Series for each x but the values are not correct (supposed to be a smooth Fourier Series curve).
Any ideas? Any help would be greatly appreciated.
%%
%Coefficients (with 95% confidence bounds):
Ts = 1800; %Time series interval (in seconds)
x = 0:Ts:(length(WavePower.Data)*1800); %Interval (Length - 89136)
a0 = 28.46;
a1 = 21.17;
b1 = 2.302;
a2 = -0.3387;
b2 = 2.8;
a3 = -4.94;
b3 = 3.124;
w = 0.000353;
% Goodness of fit:
% SSE: 1.862e+08
% R-square: 0.106
% Adjusted R-square: 0.106
% RMSE: 45.71
%General model Fourier3:
yfit = a0 + a1*cos(x*w) + b1*sin(x*w) + a2*cos(2*x*w) + b2*sin(2*x*w) + a3*cos(3*x*w) + b3*sin(3*x*w);
% Compute the two-sided spectrum P2. Then compute the single-sided spectrum P1 based...
T = 60*30; %Sampling period
Fs = 1/T; %Sampling frequency
L = length(WavePower.Data); %Length of singal (89136) - no inputs
t = (0:L-1)*T; %Time vector
Y = fft(yfit); %Where yfit is your Fourier Series
%Define the frequency domain f and plot the single-sided amplitude spectrum P1. The a.........
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L; %Frequency domain
plot(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Cycles/30 minutes)')
ylabel('|P1(f)|')

Answers (1)

VBBV
VBBV on 27 Mar 2022
plot(yfit(1:Ts:end)) % plot your fourierseries
It seems you have already computed the fourier series, To visualize the smooth curve that you're referring plot only yfit

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!