简体   繁体   English

在使用MATLAB将傅立叶级数拟合到离散数据之前,如何定义周期?

[英]How can I define the period before fitting a Fourier series to discrete data using MATLAB?

I'm using MATLAB's fit function: 我正在使用MATLAB的fit函数:

fourier_series=(x,y,'fourier8'); fourier_series =(X,Y, 'fourier8');

to fit an 8th order Fourier series to a set of discrete data (x,y). 使8阶傅立叶级数适合一组离散数据(x,y)。 I need the period of the Fourier series to be 2*pi. 我需要傅立叶级数的周期为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. 问题的背景:我正在分析骑自行车者的脚蹬的视频捕获数据,这些数据随着时间的推移以3D空间中的关节位置云输出。 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 (ie top dead centre, TDC) and the crank arm angle is 2*pi (ie TDC after one crank arm rotation). 因此,必须将傅里叶级数的周期限制为2 * pi,因为当曲柄角度为零(即上止点,TDC)且曲柄角度为零时,“平均”位置必须返回到同一位置。 2 * pi(即曲柄臂旋转一圈后的TDC)。

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. 当前,MATLAB将周期选择为略大于2 * pi,这意味着当我使用“傅立叶级数”来计算骑车人的位置时,在连续的踏板行程中,对于相同的曲柄角度,骑车人的位置会发生变化。

The best way to force the fit function on a certain period is to resort to a custom equation model, via fittype . 在特定时间段强制fit函数的最佳方法是通过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 . 另一个选择(将会发出警告)是将参数w上下限固定为相同的值,然后选择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');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM