Combine or Superpose 151 Sine Waves?

Using the below code and I have been able to successfully created 151 different sine waves all fit to my data set.
y = Score(:);
n = 501;
t = (1:501)';
games = 1:501;
data(1:151) = struct('X',NaN(501,3),'bhat',NaN(3,1),'yhat',NaN);
for ii = 1:151
tmp = 2*pi*(sincos(ii))*t;
data(ii).X = rand(501,3);
data(ii).X(:,2) = cos(tmp)';
data(ii).X(:,3) = sin(tmp)';
data(ii).bhat = data(ii).X\y;
data(ii).yhat = data(ii).bhat(1)+data(ii).bhat(2)*cos(tmp)+data(ii).bhat(3)*sin(tmp);
end
My question is how do I combine or superpose all 151 sine waves into one sine wave?
Thanks!!

 채택된 답변

Walter Roberson
Walter Roberson 2012년 5월 3일

0 개 추천

Guessing about which field you are referring to:
sum(horzcat(data.yhat),2) ./ 151

추가 답변 (1개)

Clifford Shelton
Clifford Shelton 2012년 5월 4일

0 개 추천

Thanks a bunch. Both of the suggestions work. But now it seems that the single sin wave created is extremely thin. My data set has values that range between 0-20.
and each of the 151 sine waves constructed separately also have amplitudes that fit the range of values.
When the sin waves are all added together using your above suggestion..the single sine wave only has a value range between 5-5.4. This makes the newly constructed single sine wave look like a small tightrope when plotted with my data set and isn't very useful visually to see the best fit.
Do you have any idea how to remedy that problem?

댓글 수: 8

Walter Roberson
Walter Roberson 2012년 5월 4일
Let
yhat = horzcat(data.yhat);
and then ask
std(yhat,2)
and
max(yhat,[],2) - min(yhat,[],2)
Clifford Shelton
Clifford Shelton 2012년 5월 4일
I'm not exactly sure how to apply what you are suggesting.
when I do:
std(yhat,2) I get an error:
??? Error using ==> var at 95
W must be a vector of nonnegative weights, or a scalar 0 or 1.
Error in ==> std at 32
y = sqrt(var(varargin{:}));
Something isn't working.
Furthermore, when I try to plot even after ignoring the errors...the sinewaves don't consolidate into one.
Walter Roberson
Walter Roberson 2012년 5월 4일
Sorry I tend to forget that weight argument. Try
std(yhat,[],2)
Except now that I think of it, that isn't going to give you useful information. Ummm, skip that one for now.
This and the max-min are intended to probe the reasonableness of such a tight range of sums.
Reminder: Using the yhat field was a guess on my part as to which field (if any) holds the fitted sine wave.
Clifford Shelton
Clifford Shelton 2012년 5월 4일
you were correct in guessing the yhat field hold the fitted sine waves.
The std calculation now works..but I just get 0 as the answer. not very useful as you guessed.
There must be a way around this. Because each of the 151 sine waves alone all have max and mins ranging between 0-20 just like the data set. But after combining them, the one single sin wave has a min of 5.5361 and a max of 5.6812. very thin!
Your help is MOST appreciated!
Walter Roberson
Walter Roberson 2012년 5월 4일
Did you try the difference I suggested earlier, max(yhat,[],2) - min(yhat,[],2)
Clifford Shelton
Clifford Shelton 2012년 5월 4일
I'm a little confused as to how to combine the two operations. Could you give me example code as to how I should
1.) combine all the sine waves
2.) then change their max and min.
Here is what I have been doing..but I'm missing a step:
>> yhat = sum(horzcat(data.yhat),2) ./151;
>> yhat = horzcat(data.yhat); <this doesnt' make sense>
>> std(yhat,[],2);
>> max(yhat,[],2)-min(yhat,[],2);
Umm...this is clearly wrong. Please help!
Walter Roberson
Walter Roberson 2012년 5월 4일
The std() and max/min calls are just for information to try to figure out the problem. You can remove the std() call.
Remove the existing line
yhat = sum(horzcat(data.yhat),2) ./151;
and use
yhat_array = horzcat(data.yhat);
yhat = mean(yhat_array,2);
and then for information purposes
max(yhat_array, [], 2) - min(yhat_array, [], 2)
with no semi-colon
Clifford Shelton
Clifford Shelton 2012년 5월 4일
hm..that gives me a max of 1.67 and a min of 1.12.
that didn't seem to do what we had planned.
I'm not exactly understanding how if the 151 sine waves standing alone have a wider range in values...why does that all of a sudden shrink when they are added together?
Shouldn't the sums of them create even higher and lower max and mins on the sine waves?
What would be a way for me to just do simple addition of each sine wave from the scalar structure? I've done that in the past with sinewaves and it created desired results.
You are the MAN by the way!

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by