-1

i would like to understand one thing and please help me to clarify it,sometimes it is necessary to represent given data by sum of complex exponentials with additive white noise,let us consider following model using sinusoidal model

clear all;
A1=24;
A2=23;
A3=23;
A4=23;
A5=10;
 f1=11.01;
 f2=11.005;
 f3= 10;
 f4=10.9
 phi=2*pi*(rand(1,4)-0.5);
 t=0:0.01:2.93;
 k=1:1:294;
x=rand([1,length(t)]);
 y(k)=A1.*sin(2*pi*f1*t+phi(1))+A2.*cos(2*pi*f2*t+phi(2))+A3.*sin(2*pi*f3*t+phi(3))+A4.*cos(2*pi*f4*t+phi(4))+A5.*x;
 [pxx,f]=periodogram(y,[],[],100);
 plot(f,pxx)

there phases are distributed uniformly in range of [-pi pi],but my main question is related following fact.to represent data as linear combination of complex exponentials with phases uniformly distributed in [-pi pi] interval, should we generate these phases outside of sampling or at each sampling process we should generate new list of phases?please help me to clarify this things

  • what is a reason of downvoting –  May 05 '14 at 19:07
  • 2
    I haven't downvoted, but I can understand the person who did it. In my opinion, this question is poorly phrased, contains both too little and too much information at the same time, and it's hard to understand exactly what you're asking. – Stewie Griffin May 05 '14 at 19:33
  • Does this line compile? `[pxx,f]=periodogram(y,[],[],100);` I am receiving an error. – NKN May 05 '14 at 19:36
  • why it is difficult?we have uniformly generated phases right? –  May 05 '14 at 19:43
  • please read question carefully maybe English is bad,but i am asking should i generate this phases uniformly at once or i should generate new set of phases at each iteration,what is unclear? –  May 05 '14 at 19:45
  • in simple languages code phi=2*pi*(rand(1,4)-0.5); should be outside of t vectorization or inside? –  May 05 '14 at 19:46

1 Answers1

2

As I stated in the my comment, I don't really understand what you're asking. But, I will answer this as if you had asked it on codereview.

The following is not good practice in MATLAB:

A1=24;
A2=23;
A3=23;
A4=23;
A5=10;

There are very few cases (if any), where you actually need such variable names. Instead, the following would be much better:

A = [24 23 23 23 10];

Now, if you want to use A1, you do A(1) instead.

These two lines:

t=0:0.01:2.93;
k=1:1:294;

They are of course the same size (1x294), but when you do it that way, it's easy to get it wrong. You will of course get errors later on if they're not the same size, so it's nice to make sure that you have it correct on the first try, thus using linspace might be a good idea. The following line will give you the same t as the line above. This way it's easier to be sure you have exactly 294 elements, not 293, 295 or 2940 (it is sometimes easy to miss).

t = linspace(0,2.93,294);

Not really important, but k = 1:1:294 can be simplified to k = 1:294, as the default step size is 1.

The syntax .*, is used for element-wise operations. That is, if you want to multiply each element of a vector (or matrix) with the corresponding element in another one. Using it when multiplying vectors with scalars is therefore unnecessary, * is enough.

Again, not an important point, but x=rand([1,length(t)]); is simpler written x=rand(1, length(t)); (without brackets).

You don't need the index k in y(k) = ..., as k is continuous, starting at 1, with increments of 1. This is the default behavior in MATLAB, thus y = ... is enough. If, however, you only wanted to fill in every other number between 1 and 100, you could do y(1:2:100).

This is far from perfect, but in my opinion big step in the right direction:

A = [24 23 23 23 10];
f = [11.01 11.005 10 10.9];   % You might want to use , as a separator here
phi = 2*pi*(rand(1,4)-0.5);
t = linspace(0,2.93,294);
x = rand(1, length(t));

w = 2*pi*f;   % For simplicity
y = A(1)*sin(w(1)*t+phi(1)) + A(2)*cos(w(2)*t+phi(2)) + ...
    A(3)*sin(w(3)*t+phi(3)) + A(4)*cos(w(4)*t+phi(4))+A(5)*x;

Another option would be:

z = [sin(w(1)*t+phi(1)); cos(w(2)*t+phi(2)); sin(w(3)*t+phi(3)); ...
    cos(w(4)*t+phi(4)); x];
y = A.*z;

This will give you the same y as the first one. Having the same w, t and phi as above, the following will also give you the same results:

c = bsxfun(@times,w,t') + kron(phi,ones(294,1));
y = sum(bsxfun(@times,A,[sin(c(:,1)), cos(c(:,2)), sin(c(:,3)), cos(c(:,4)), x']),2)';

I hope something in here might help you some in your further work. And maybe I actually answered your question. =)

Community
  • 1
  • 1
Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
  • +1 for giving such a good quality answer to a not-so-good-quality question. – Cape Code May 05 '14 at 20:47
  • @Robert P thanks for matlab syntax,it is more professional of course,but that is not my question,look please i have random phases yes?generally there exist models which are sinusoidal with phases uniformly distributed right?i am asking to get this model correctly,should i generate they at once and insert into model or at each iteration i should generate new sample?is it so unclear?in which language should i talk? :( –  May 06 '14 at 07:08
  • in other word,if i took loop instead of vectorization,should i generate this random sample of phases at each iteration of loop? –  May 06 '14 at 07:10
  • Honestly, I don't know, but I think vectorization would be the best approach. I don't understand this well enough to give you a better answer than that I'm afraid. – Stewie Griffin May 06 '14 at 07:29
  • let us continue it to chat ok? –  May 06 '14 at 17:30