Question

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
Was it helpful?

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

OTHER TIPS

rand(1,t) generates 1 by t matrix, to get a scalar value you can use rand(1,1)

Note that sin and cos accept vector arguments, so there is no need for a for loop,

T = 1 / fs;
t = (0:N-1)' * T; 
WN = rand(N, 1) * 2; 
WN = WN - mean(WN); 
x = 20 * sin(2*pi*f1*t) + 30 * cos(2*pi*f2*t) + A3 * WN;

Additionally, your value for T should be 1 / sample rate, not 1 / f1 (unless you are sampling at this frequency, in which case you cannot expect to see the right results because your frequencies are BOTH above the Nyquist frequency of fs / 2, so there will be aliasing). Also, you should be subtracting the mean of the whole vector WN, not just blindly assuming that the mean is 0.5 (or 1, once it's been multiplied by 2).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top