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(s​igma,t)

2 views (last 30 days)
deltat=0.1;
nsamples=2^10;
fs=1/deltat;
time=[0 : deltat : deltat*(nsamples-1)]
time = 1x1024
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
figure(1);
plot(time,ydata);
  3 Comments
Byungho
Byungho on 4 Apr 2024
Edited: Byungho on 4 Apr 2024
I'm trying to generate Gaussian noise from x(t)=5+10*cos(2*pi*t+pi/6)+G(sigma,t)
G(sigma,t) is Gaussian noise with a standard deviation of 15 (sigma G=15), detat=0.1(s), and nsample=2^10.
It is the start of calculating Power Spectral Density with FFT.
The overall variance of the signal is (sigma g)^2+(A)^2/2 as expected.
The generated random signal should be sinusoidal because X(t) has the cosine factor.
Byungho
Byungho on 4 Apr 2024
deltat=0.1;
nsamples=2^10;
fs=1/deltat;
time=[0 : deltat : deltat*(nsamples-1)]
time = 1x1024
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
psdtot(1:nsamples/2+1) = zeros(1,nsamples/2+1);
nblock = 10;
for k=1:nblock
for i=1: nsamples
ydata(i) =randn(1);
end
[pxx,f] = periodogram(ydata,rectwin(nsamples),nsamples,fs);
psdtot = psdtot + pxx / nblock;
end
figure(1);
plot(time,ydata);
The above codes are the Gaussian noise without the signal bias and the sinusoidal signal.

Sign in to comment.

Accepted Answer

Ayush Anand
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!
  1 Comment
Byungho
Byungho on 4 Apr 2024
deltat=0.1;
nsamples=2^10;
fs=1/deltat;
time=[0 : deltat : deltat*(nsamples-1)]
time = 1x1024
0 0.1000 0.2000 0.3000 0.4000 0.5000 0.6000 0.7000 0.8000 0.9000 1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000 2.2000 2.3000 2.4000 2.5000 2.6000 2.7000 2.8000 2.9000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
Arrays have incompatible sizes for this operation.
figure(1);
plot(time,ydata);
Could you let me know how to predefine an nsamples x 1024 dimension array?
Thank you for your input.

Sign in to comment.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!