Could you fix this code? I am trying to generate the Gaussian noise that is changing over time. X(t)=5+10*cos(2*pi*t+pi/6)+G(sigma,t)
2 views (last 30 days)
Show older comments
deltat=0.1;
nsamples=2^10;
fs=1/deltat;
time=[0 : deltat : deltat*(nsamples-1)]
psdtot(1:nsamples/2+1) = zeros(1,nsamples/2+1);
nblock = 10;
for k=1:nblock
for i=1: nsamples
ydata(i) =5+10*cos(2*pi*time+pi/6)+15* randn(1);
end
[pxx,f] = periodogram(ydata,rectwin(nsamples),nsamples,fs);
psdtot = psdtot + pxx/nblock;
end
figure(1);
plot(time,ydata);
3 Comments
Accepted Answer
Ayush Anand
on 4 Apr 2024
Hi Byungho,
The error you are getting is because you are trying to assign a vector 5+10*cos(2*pi*time+pi/6)+15*randn(1) to a single array index y(i). Since time is a vector of shape 1 x 1024, the expression 5+10*cos(2*pi*tim+pi/6) is also a vector with the same dimensions, and can't be assigned to y(i), a single array element. You might want to use cells or multidimensional arrays for the same. For that, you would need to predefine an nsamples x 1024 dimension array beforehand and then assign the values using the colon operator like y(i,:).
You can read more about the usage of the colon operator in MATLAB here:
Hope this helps!
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!