Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Sin Cos calculation for a regression

Asked by Clifford Shelton on 1 May 2012

My aim is to best fit sin waves to a dataset. I am using a data set with 501 days of data with a sample rate of 1 day.

After doing a spectral analysis on the data set (using a separate program) the following cycles have been identified:

31.19552 day cycle 5.87064 day cycle 8.00192 day cycle

Previously on this board I received help on how to code a similar task with a cycle of 3.5678 days.:

   y = data(:);
   X = ones(501,3);
   n = 0:500;
   X(:,2) = cos(2*pi*0.2794*n)';
   X(:,3) = sin(2*pi*0.2794*n)';
   betahat = X\y;

Fit is

   yhat = betahat(1)+betahat(2)*cos(2*pi*0.2794*n)+betahat(3)*sin(2*pi*0.2794*n);

My question is: how do I calculate what I am supposed to multiply pi with for both the sin and cosine equations used to complete the regression. In the above example a 3.5678 day cycle has .2794 used in the above equations.

I only partially understand this to be because 140/501 creates the most accurate number = .2794. However a simpler calculation of 1/3.5678 or .2803 could be used without any major significance to the outcome.

But what should I do for the 31.19552 day cycle 5.87064 day cycle 8.00192 day cycle?? is there some easy calculation process that I don't know about?

I'm a beginner...any code or insights would be most helpful! MAJOR props to Wayne for his on going help with this!

0 Comments

Clifford Shelton

Products

No products are associated with this question.

1 Answer

Answer by Wayne King on 1 May 2012
Accepted answer

The (2*pi) is just because you are converting the frequencies from cycles\day into radians\day. When you then multiply that by days, you get radians, which is the input argument for cos() and sin().

If you want to form a regression model for those other frequencies. You have a couple options.

1.) Simply add them as columns to your design matrix, X. For each frequency, you want to add two columns, one for the cosine and one for the sine.

2.) You can get the estimates directly from the output of fft(). The latter does not require specifying the design matrix, but the former is easier if you are using regress() or LinearModel.fit

0 Comments

Wayne King

Contact us