Gaussian Noise or Random Number¶

In [14]:
import numpy as np
import numpy.fft as fft
import matplotlib.pyplot as plt
In [10]:
Ns=1000
ons=np.ones(Ns)
fons=fft.fft(ons)
In [11]:
fons[0]/Ns
Out[11]:
(1+0j)

Noise¶

Guassian and unifrom

In [99]:
r1=np.random.randn(1000)
r2=r1*np.sqrt(5)
In [100]:
plt.plot(r1, 'r*')
plt.plot(r2, 'go')

plt.axhline(y=0, color='k')
plt.axhline(y=3, color='k')
plt.axhline(y=-3, color='k')
Out[100]:
<matplotlib.lines.Line2D at 0x7fe9da599c70>

How to find the probability distribution function¶

In [30]:
r1= np.random.randn(10000)
(p1)=plt.hist(r1, 40)
In [43]:
pdf=p1[0]; x=x[:-1]
In [57]:
plt.plot(x, pdf,'r')
plt.plot(x, 2000/np.sqrt(2*np.pi)*np.exp(-0.5*x*x),'g')
Out[57]:
[<matplotlib.lines.Line2D at 0x7fea154697c0>]
In [108]:
Ns=10000
r1=np.random.randn(Ns)*np.sqrt(5);
fr1=fft.fft(r1)/Ns;
In [109]:
(p2)=plt.hist(np.real(fr1), 30)
In [110]:
plt.plot(p1[0], 'r', label='Time domain')
plt.plot(p2[0], 'b', label='Freq domain')
Out[110]:
[<matplotlib.lines.Line2D at 0x7fea3074a5b0>]
In [121]:
(f,ax)=plt.subplots(1,2)
ax[0].plot(r1, 'r.')
ax[1].plot(np.real(fr1), 'b.')
Out[121]:
[<matplotlib.lines.Line2D at 0x7fe9d65ea6d0>]
In [125]:
import numpy as np
import numpy.fft as fft
import matplotlib.pyplot as plt
## way to find thee normalisation constant is by taking fft of ones. Since the it is constant or DC in the time domain
## we have only o th frequency coefficient, that  need to be made 1
x=np.ones(1000)
fx=fft.fft(x)
## it is 1000 for oth freq and rest are zeros. Number if time domain samples are 1000  that would be normalising factor
##print(fx)
pi=np.pi #!
sps=300.0 # Hz
dt=1/sps # sec
f1=5
f2=50.0
f3=100.0
Tobs1=0.5
Tobs2=5.0
Tobs3=10.0
N1=int(Tobs1*sps)
N2=int(Tobs2*sps)
N3=int(Tobs3*sps)
x1=np.linspace(0, Tobs1, N1)
x2=np.linspace(0, Tobs2, N2)
x3=np.linspace(0, Tobs3, N3)
y1=5*np.sin(2*pi*f1*x1)+3*np.sin(2*pi*f2*x1)+8*np.sin(2*pi*f3*x1)
y2=5*np.sin(2*pi*f1*x2)+3*np.sin(2*pi*f2*x2)+8*np.sin(2*pi*f3*x2)
y3=5*np.sin(2*pi*f1*x3)+3*np.sin(2*pi*f2*x3)+8*np.sin(2*pi*f3*x3)
## here we normalise by deviding fft results
fy1=2*fft.fft(y1)  ## 2 because we are ignoring the power in the -ve frequency
fy2=2*fft.fft(y2)  ## 2 because we are ignoring the power in the -ve frequency
fy3=2*fft.fft(y3)  ## 2 because we are ignoring the power in the -ve frequency
## lets plot data 100 data samples only there is too much data in 10 sec

## Lets compute frequency array
## We ignore the N/2 to N and consider only +v Frequency  Coefficients 
df1=1/Tobs1
df2=1/Tobs2
df3=1/Tobs3

freq1=np.arange(0,N1//2)*df1
freq2=np.arange(0,N2//2)*df2
freq3=np.arange(0,N3//2)*df3
plt.figure(figsize=[12,9])
plt.plot(freq1,np.abs(fy1[:N1//2]), label='Tobs=1 sec')
plt.plot(freq2,np.abs(fy2[:N2//2]), label='Tobs=5 sec')
plt.plot(freq3,np.abs(fy3[:N3//2]), label='Tobs=10 sec')
plt.xlabel('Frequency [Hz]' )
plt.ylabel('Fourier Transform')
plt.title('Foruier Domain')
plt.axvline(x=5, color='r', ls='--', lw=0.5)
plt.text(5, -.2, 'f=5Hz', color='r')
plt.axvline(x=50, color='r', ls='--', lw=0.5)
plt.text(50, -.2, 'f=50Hz', color='r')
plt.axvline(x=100, color='r', ls='--', lw=0.5)
plt.text(100, -.2, 'f=100Hz', color= 'r')
plt.grid(alpha=0.3, color='c')
plt.legend()
Out[125]:
<matplotlib.legend.Legend at 0x7fe9d6ef4df0>
In [134]:
import numpy as np
import numpy.fft as fft
import matplotlib.pyplot as plt
## way to find thee normalisation constant is by taking fft of ones. Since the it is constant or DC in the time domain
## we have only o th frequency coefficient, that  need to be made 1

pi=np.pi #!
sps=300.0 # Hz
dt=1/sps # sec

Tobs1=0.5
Tobs2=5.0
Tobs3=10.0
N1=int(Tobs1*sps)
N2=int(Tobs2*sps)
N3=int(Tobs3*sps)
x1=np.linspace(0, Tobs1, N1)
x2=np.linspace(0, Tobs2, N2)
x3=np.linspace(0, Tobs3, N3)
y1=5*np.sin(2*pi*f1*x1)+3*np.sin(2*pi*f2*x1)+8*np.sin(2*pi*f3*x1)
y2=5*np.sin(2*pi*f1*x2)+3*np.sin(2*pi*f2*x2)+8*np.sin(2*pi*f3*x2)
y3=5*np.sin(2*pi*f1*x3)+3*np.sin(2*pi*f2*x3)+8*np.sin(2*pi*f3*x3)
y1=y1+15*np.random.randn(N1)
y2=y2+15*np.random.randn(N2)
y3=y3+15*np.random.randn(N3)
## here we normalise by deviding fft results
fy1=2*fft.fft(y1)  ## 2 because we are ignoring the power in the -ve frequency
fy2=2*fft.fft(y2)  ## 2 because we are ignoring the power in the -ve frequency
fy3=2*fft.fft(y3)  ## 2 because we are ignoring the power in the -ve frequency
## lets plot data 100 data samples only there is too much data in 10 sec

## Lets compute frequency array
## We ignore the N/2 to N and consider only +v Frequency  Coefficients 
df1=1/Tobs1
df2=1/Tobs2
df3=1/Tobs3

freq1=np.arange(0,N1//2)*df1
freq2=np.arange(0,N2//2)*df2
freq3=np.arange(0,N3//2)*df3
plt.figure(figsize=[12,9])
plt.plot(freq1,np.abs(fy1[:N1//2]), label='Tobs=1 sec')
plt.plot(freq2,np.abs(fy2[:N2//2]), label='Tobs=5 sec')
plt.plot(freq3,np.abs(fy3[:N3//2]), label='Tobs=10 sec')
plt.xlabel('Frequency [Hz]' )
plt.ylabel('Fourier Transform')
plt.title('Foruier Domain')
plt.axvline(x=5, color='r', ls='--', lw=0.5)
plt.text(5, -.2, 'f=5Hz', color='r')
plt.axvline(x=50, color='r', ls='--', lw=0.5)
plt.text(50, -.2, 'f=50Hz', color='r')
plt.axvline(x=100, color='r', ls='--', lw=0.5)
plt.text(100, -.2, 'f=100Hz', color= 'r')
plt.grid(alpha=0.3, color='c')
plt.legend()

plt.figure(figsize=[12,9])
plt.plot(x1, y1, 'r', label='0,5 Sec')
plt.plot(x2, y2, 'b', label='5 Sec')
plt.plot(x3, y3, 'c', label='10 Sec')
plt.legend()
Out[134]:
<matplotlib.legend.Legend at 0x7fe9dd8c92b0>
In [128]:
import numpy as np
import numpy.fft as fft
import matplotlib.pyplot as plt
## way to find thee normalisation constant is by taking fft of ones. Since the it is constant or DC in the time domain
## we have only o th frequency coefficient, that  need to be made 1

pi=np.pi #!
sps=300.0 # Hz
dt=1/sps # sec

Tobs1=0.5
Tobs2=5.0
Tobs3=10.0
N1=int(Tobs1*sps)
N2=int(Tobs2*sps)
N3=int(Tobs3*sps)

y1=np.random.randn(N1)
y2=np.random.randn(N2)
y3=np.random.randn(N3)
## here we normalise by deviding fft results
fy1=2*fft.fft(y1)  ## 2 because we are ignoring the power in the -ve frequency
fy2=2*fft.fft(y2)  ## 2 because we are ignoring the power in the -ve frequency
fy3=2*fft.fft(y3)  ## 2 because we are ignoring the power in the -ve frequency
## lets plot data 100 data samples only there is too much data in 10 sec

## Lets compute frequency array
## We ignore the N/2 to N and consider only +v Frequency  Coefficients 
df1=1/Tobs1
df2=1/Tobs2
df3=1/Tobs3

freq1=np.arange(0,N1//2)*df1
freq2=np.arange(0,N2//2)*df2
freq3=np.arange(0,N3//2)*df3
plt.figure(figsize=[12,9])
plt.plot(freq1,np.abs(fy1[:N1//2]), label='Tobs=1 sec')
plt.plot(freq2,np.abs(fy2[:N2//2]), label='Tobs=5 sec')
plt.plot(freq3,np.abs(fy3[:N3//2]), label='Tobs=10 sec')
plt.xlabel('Frequency [Hz]' )
plt.ylabel('Fourier Transform')
plt.title('Foruier Domain')
plt.axvline(x=5, color='r', ls='--', lw=0.5)
plt.text(5, -.2, 'f=5Hz', color='r')
plt.axvline(x=50, color='r', ls='--', lw=0.5)
plt.text(50, -.2, 'f=50Hz', color='r')
plt.axvline(x=100, color='r', ls='--', lw=0.5)
plt.text(100, -.2, 'f=100Hz', color= 'r')
plt.grid(alpha=0.3, color='c')
plt.legend()
Out[128]:
<matplotlib.legend.Legend at 0x7fe9d69d8310>
In [ ]: