Search code examples
matlabnoiseperiodspectral

Fit sinusoidal models


I have tried to fit following sinusoidal model by the following model, which means that deterministic model + white noise:

function [x]=generate(N,m,A3)
f1=100;
f2=200;
T=1/f1;
for t=0:N*T/m:N*T
    WN(t)=rand(1,t)*2-1;
    x(t)=20*sin(2*pi*f1*t)+30*cos(2*pi*f2*t)+A3*WN(t);
end;

But when I run using following command:

generate(100,10,40)

I got following error:

Subscript indices must either be real positive integers or logicals.

Error in generate (line 6)
    WN(t)=rand(1,t)*2-1;

So what is wrong? As I know I can use two parameters for rand function right? What is problem here?

UPDATED: that is what i have done for fixing it

function [ x]=generate(N,m,A3)
WN=[];
f1=100;
f2=200;
T=1/f1;
k=0;
for t=0:N*T/m:N*T
   % WN(k)=rand(1,1)*2-1;
   % x(k)=20*sin(2*pi*f1*t)+30*cos(2*pi*f2*t)+A3*WN(k);
    %k=k+1;
    WN=rand*2-1;
     disp(20*sin(2*pi*f1*t)+30*cos(2*pi*f2*t)+A3*WN);
end;

and result

 generate(1000,10,40)
   25.4143

   -1.4678

   66.9518

   -9.6293

   51.9928

   55.3843

   59.4956

   -3.2451

   21.9826

   10.7896

   54.0055

Solution

  • I have solved it as follows:

    function x = generate(N,m,A3)
    f1 = 100;
    f2 = 200;
    T = 1./f1;
    t = (0:(N*T/m):(N*T))';
    wn = rand(length(t),1).*2 - 1;
    x = 20.*sin(2.*pi.*f1.*t) + 30.*cos(2.*pi.*f2.*t) + A3.*wn;
    end