我的任务是通过最小二乘法从多年的一组温度测量中消除年度和半年度的波动。
我找到了这里描述的方法,由于我必须等待我的同事首先对数据进行一些分析并将它们传递给我,所以我决定在 Matlab 中进行测试运行,使用具有两个简单正弦和余弦函数的信号和随机噪声。
%% Creating the data
g=2.5;
t=linspace(0,g,g*365*24*60);              %n-years of 1min data
o=rand(size(t));                         %random noise
p=2*sin(2*pi*t)+1.5*cos(pi*t);           %we create two sinusoids  
x=p+o;                                     %and add noise to them
%% The method
N=length(x);            %no of data
n=(1:N);
T=g*365*24*60;          %time that the data covers
f1=1/(365*24*60);       %frequency of 1 yr oscillations
f2=1/((365*24*60)/2);   %frequency of 1/2 year oscillations
a1=f1*T;                
a2=f2*T;
A1=(2/N)*sum(x.*cos((2*pi*a1*n)/N));
A2=(2/N)*sum(x.*cos((2*pi*a2*n)/N));
B1=(2/N)*sum(x.*sin((2*pi*a1*n)/N));
B2=(2/N)*sum(x.*sin((2*pi*a2*n)/N));
C1=sqrt(A1^2+B1^2);                     %amplitude 
C2=sqrt(A2^2+B2^2);
fi1=atan(B1/A1);                        %phase shift
fi2=atan(B2/A2);
v=(C1*cos(2*pi*t-fi1))+(C2*cos(2*pi*t-fi2)); %the reconstructed set of data
                                             for these two frequencies
r=x-v;    %I wasn't sure what was meant by removing, so i just subtracted
%% Visualization
figure                                  %now lets plot the data
plot(t,x)
xlabel('t[min]')
ylabel('Arb. units :)')
title('Data')
figure                           %and, original signal and noise separately
plot(t,p,'b')
hold on
plot(t,o,'-r')
xlabel('t[min]')
ylabel('Arb. units :)')
title('Data separated')
figure                    %and once again data (blue), reconstructed signal 
plot(t,x,'b-')            %(green), and the final result afer subtraction
hold on                   %(red)
plot(t,v,'g--')
plot(t,r,'r:')
xlabel('t[min]')
ylabel('w/e')
现在的问题是,重建的信号 (v) 与原始信号不太相似。即使我消除了噪声并只使用纯正弦信号,我得到的也不是它的一个很好的表示。所以,我最感兴趣的是:
- 这是对这种方法的正确使用吗?我使用了正确的方程式吗?
 - 代码中的某处有错误吗?由于错误,我主要考虑错误的定义或某些变量的计算。
 - 在这种“理想情况”(无噪声的正弦曲线)中,该方法是否应该能够(几乎)准确地再现原始曲线?
 
我希望我已经清楚易懂地阐述了我的问题,如果有人有其他问题,请提出。我知道这是相当基本的东西,但我在这方面的知识和经验非常有限(最多不存在),所以请多多包涵。预先感谢您的任何帮助。