We are sampling a data at a rate 300 Hz. The data involves sine three frequencies 20 Hz, 50 Hz and 100 Hz
with amplitude 5,3 and 8 respectively
The maximum frequency is 100Hz and sampling rate is 300 Hz and fine as per Nyquist sampling theorem.
\begin{equation}
s= 5 \times \sin(2 \pi \, 20 \, t) + 3 \times \sin( 2 \pi \, 50 \, t) + 8 \sin( 2 \pi \, 100 \, t)
\end{equation}
We take data for 10 sec. Total sames are N= sps*Tobs
The output of fft array arrangements
import numpy as np
import numpy.fft as fft
import matplotlib.pyplot as plt
pi=np.pi #!
sps=300.0 # Hz
dt=1/sps # sec
f1=20.0
f2=50.0
f3=100.0
Tobs=10.0
N=int(Tobs*sps)
x=np.linspace(0, Tobs, N)
y=5*np.sin(2*pi*f1*x)+3*np.sin(2*pi*f2*x)+8*np.sin(2*pi*f3*x)
fy=fft.fft(y)
## lets plot data 100 data samples only there is too much data in 10 sec
f1=plt.figure(figsize=[12,9])
plt.plot(x[:100], y[:100])
plt.xlabel('time [sec]')
plt.ylabel('signal [sec]')
plt.title('Time Domain')
##
## Lets compute frequency array
## We ignore the N/2 to N and consider only +v Frequency Coefficients
df=1/Tobs
f=np.arange(0,N//2)*df
print( f[1]-f[0], 1/Tobs)
f2=plt.figure(figsize=[12,9])
plt.plot(f,np.abs(fy[:N//2]))
plt.xlabel('Frequency [Hz]' )
plt.ylabel('Fourier Transform')
plt.title('Foruier Domain')
plt.axvline(x=20, color='r', ls='--', lw=0.5)
plt.text(20, -300, 'f=20Hz', color='r')
plt.axvline(x=50, color='r', ls='--', lw=0.5)
plt.text(50, -300, 'f=50Hz', color='r')
plt.axvline(x=100, color='r', ls='--', lw=0.5)
plt.text(100, -300, 'f=100Hz', color= 'r')
plt.grid(alpha=0.3, color='c')
0.1 0.1
Let's use the function \begin{equation} s(t)= 5 \times \sin(2 \pi \, 20 \, t) + 3 \times \sin( 2 \pi \, 50 \, t) + 8 \sin( 2 \pi \, 100 \, t) \end{equation} and a sampling rate sps=300 Hz with a total observation time of 10 sec!
import numpy as np
import numpy.fft as fft
import matplotlib.pyplot as plt
pi=np.pi #!
sps=300.0 # Hz
dt=1/sps # sec
f1=20.0
f2=50.0
f3=100.0
Tobs=10.0
N=int(Tobs*sps)
x=np.linspace(0, Tobs, N)
f=np.arange(1,N//2)*df
y=5*np.sin(2*pi*f1*x)+3*np.sin(2*pi*f2*x)+8*np.sin(2*pi*f3*x)
fy=fft.fft(y)
coeff_ptive=fy[1:N//2]
coeff_ntive=np.flip(fy[N//2+1:N])
## idex zero is constant we dont need to check
## We need to go to all the way till N//2-1
print(len(coeff_ptive), len(coeff_ntive), len(f))
#plt.plot(f, coeff_ptive-np.conj(coeff_ntine))
#plt.plot(np.real(coeff_ptive[150:250]),'r')
#plt.plot(np.real(coeff_ntive[150:250]), 'b')
plt.plot(f,np.real(coeff_ptive)-np.real(coeff_ntive),'C0', label='Real')
plt.plot(f,np.imag(coeff_ptive)+np.imag(coeff_ntive),'C1', label='Imag')
plt.xlabel('Frequency [Hz]')
plt.legend()
plt.grid(alpha=0.3, color='c')
plt.title('To prove the property of real function')
#plt.plot(,'b')
1499 1499 1499
Text(0.5, 1.0, 'To prove the property of real function')
We have seen in the earlier plot that Fourier domain amplitude is praportiional the time domain amplitude. However, there is a normalisation factor Once correct, it is easy to detemine the amplitude.
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=20.0
f2=50.0
f3=100.0
Tobs=10.0
N=int(Tobs*sps)
x=np.linspace(0, Tobs, N)
y=5*np.sin(2*pi*f1*x)+3*np.sin(2*pi*f2*x)+8*np.sin(2*pi*f3*x)
## here we normalise by deviding fft results
fy=2*fft.fft(y)/N ## 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
df=1/Tobs
f=np.arange(0,N//2)*df
print( f[1]-f[0], 1/Tobs)
f2=plt.figure(figsize=[12,9])
plt.plot(f,np.abs(fy[:N//2]))
plt.xlabel('Frequency [Hz]' )
plt.ylabel('Fourier Transform')
plt.title('Foruier Domain')
plt.axvline(x=20, color='r', ls='--', lw=0.5)
plt.text(20, -.2, 'f=20Hz', 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')
0.1 0.1
Let's see what happens if sample at 200 sps,
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=200 # Hz
dt=1/sps # sec
f1=20.0
f2=50.0
f3=100.0
Tobs=10.0
N=int(Tobs*sps)
x=np.linspace(0, Tobs, N)
y=5*np.sin(2*pi*f1*x)+3*np.sin(2*pi*f2*x)+8*np.sin(2*pi*f3*x)
## here we normalise by deviding fft results
fy=2*fft.fft(y)/N ## 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
df=1/Tobs
f=np.arange(0,N//2)*df
print( f[1]-f[0], 1/Tobs)
f2=plt.figure(figsize=[12,9])
plt.plot(f,np.abs(fy[:N//2]))
plt.xlabel('Frequency [Hz]' )
plt.ylabel('Fourier Transform')
plt.title('Foruier Domain')
plt.axvline(x=20, color='r', ls='--', lw=0.5)
plt.text(20, -.2, 'f=20Hz', 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')
0.1 0.1
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=180 # Hz
dt=1/sps # sec
f1=20.0
f2=50.0
f3=100.0
Tobs=10.0
N=int(Tobs*sps)
x=np.linspace(0, Tobs, N)
y=5*np.sin(2*pi*f1*x)+3*np.sin(2*pi*f2*x)+8*np.sin(2*pi*f3*x)
## here we normalise by deviding fft results
fy=2*fft.fft(y)/N ## 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
df=1/Tobs
f=np.arange(0,N//2)*df
print( f[1]-f[0], 1/Tobs)
f2=plt.figure(figsize=[12,9])
plt.plot(f,np.abs(fy[:N//2]))
plt.xlabel('Frequency [Hz]' )
plt.ylabel('Fourier Transform')
plt.title('Foruier Domain')
plt.axvline(x=20, color='r', ls='--', lw=0.5)
plt.text(20, -.2, 'f=20Hz', 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')
0.1 0.1
The frequency resolution depends on the observation time. We use the same data and use three Tobs = 0.5 sec, 5 sec and 10 sec while keeping sampling frequency is 300
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)/N1 ## 2 because we are ignoring the power in the -ve frequency
fy2=2*fft.fft(y2)/N2 ## 2 because we are ignoring the power in the -ve frequency
fy3=2*fft.fft(y3)/N3 ## 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()
<matplotlib.legend.Legend at 0x7fdea50829a0>