I have a data set of 501 days with a sample rate of 1 day. After doing a spectral analysis of the data I want to generate a sinwave for each identified cycle and then best fit that sin wave to my plotted data set.
the variable "sincos" is a vector column of all the values of the identified cycles that I want to multiply sin and cosine with
For example: my data set identifies 5 cycles. In the example code below I have manually typed and retyped the commands in order to generate successive X,X1,X2…etc
betahat,betahat1,…etc
yhat,yhat1,yhat2…etc
and have manually typed in the location of data in my sincos variable. sincos(1), sincos(2), sincos(3)….etc.
I know there is a better way to do this!! Code to automate this process is MOST appreciated!!
>> X = ones(501,3);X(:,2) = cos(2*pi*(sincos(1))*n)';X(:,3) = sin(2*pi*(sincos(1))*n)';betahat = X\y;yhat = betahat(1)+betahat(2)*cos(2*pi*(sincos(1))*n)+betahat(3)*sin(2*pi*(sincos(1))*n);>> X1 = ones(501,3);X1(:,2) = cos(2*pi*(sincos(2))*n)';X1(:,3) = sin(2*pi*(sincos(2))*n)';betahat1 = X1\y;yhat1 = betahat1(1)+betahat1(2)*cos(2*pi(sincos(2))*n)+betahat1(3)*sin(2*pi*(sincos(2))*n);>> X2 = ones(501,3);X2(:,2) = cos(2*pi*(sincos(3))*n)';X2(:,3) = sin(2*pi*(sincos(3))*n)';betahat2 = X2\y;yhat2 = betahat2(1)+betahat2(2)*cos(2*pi(sincos(3))*n)+betahat2(3)*sin(2*pi*(sincos(3))*n);>> X3 = ones(501,3);X3(:,2) = cos(2*pi*(sincos(4))*n)';X3(:,3) = sin(2*pi*(sincos(4))*n)';betahat3 = X3\y;yhat3 = betahat3(1)+betahat3(2)*cos(2*pi(sincos(4))*n)+betahat3(3)*sin(2*pi*(sincos(4))*n);>> X4 = ones(501,3);X4(:,2) = cos(2*pi*(sincos(5))*n)';X4(:,3) = sin(2*pi*(sincos(5))*n)';betahat4 = X4\y;yhat4 = betahat4(1)+betahat4(2)*cos(2*pi(sincos(5))*n)+betahat4(3)*sin(2*pi*(sincos(5))*n);
Thanks for all the assistance! and Wayne.. you are the MAN!
Best Answer