I'm using MATLAB's fit function:
fourier_series=(x,y,'fourier8');
to fit an 8th order Fourier series to a set of discrete data (x,y). I need the period of the Fourier series to be 2*pi. However I can't work out how to fix this so that when I call the function it fits the series to my required period. Any suggestions would be greatly appreciated. Thanks.
Background to problem: I am analysing video capture data of cyclist's pedalling which outputs as a cloud of joint positions in 3D space over time. The joint positions change slightly every pedal stroke. I am therefore wishing to fit Fourier series' to these joint positions and joint angles as a function of crank arm angle to find the cyclist's "average" position. The period of the Fourier series' therefore need to be constrained to be 2*pi as the "average" positions must return to the same location when the crank arm angle is zero (i.e. top dead centre, TDC) and the crank arm angle is 2*pi (i.e. TDC after one crank arm rotation).
Currently MATLAB is selecting the period to be slightly greater than 2*pi which means that when I use the Fourier series' to calculate the cyclist's position, the cyclist's position changes for the same crank arm angle on consecutive pedal strokes.
The best way to force the fit
function on a certain period is to resort to a custom equation model, via fittype
. Another option (that will throw a warning) is to fix the lower and upper bounds of the parameter w
to the same value, and select as solution method LinearLeastSquares
.
A cleaner solution is obtained by observing that, since you already know the period the fitting problem is linear in the parameters, and so you can resort to the linear least-squares method. I'll show hereafter an example of this approach.
%// Build a simple time series with period 2*pi.
t = (0:0.01:4*2*pi)';
y = sawtooth(t);
T = 2*pi;
%// Compute the angular speed and the azimuth.
Omega = 2*pi/T;
alpha = Omega*t;
%// Build the regressor matrix, with n harmonics.
n = 8;
A = ones(length(t), 2*n+1);
for i = 1:n
A(:,[2*i 2*i+1]) = [cos(i*alpha) sin(i*alpha)];
end
%// Solve the linear system.
%// The parameters are sorted as:
%// p = [y0 a1 b1 a2 b2 ...]'
%// being y0 the average of y, a_i the terms multiplying the sines
%// and b_i the terms multiplying the cosines.
p = A\y;
%// Evaluate the Fourier series.
yApprox = A*p;
%// Compare the solution with the data.
figure();
hold on;
plot(t, y, 'b');
plot(t, yApprox, 'r');
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments