forked from SparkAbhi/SignalProcessingWithPython
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChapter5.py
More file actions
Latest commit
83 lines (66 loc) · 3.21 KB
/
Copy pathChapter5.py
File metadata and controls
83 lines (66 loc) · 3.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Author: A K Agrawal
Email: kumarabhishekbh8@gmail.com
"""
"""
Description:
In this tutorial we will a noisy signal using FFT technique
signal is noisy sin wave for which we have some idea of it's original frequency. similar to
chapter 2. the we will create a filter, multiply filter frequency response with signal response and take
inverse FFT of result, pass through DAC
"""
fromsignalUtilityimportfunctionGenerator
fromsignalUtilityimportoscilloscope
fromsignalUtilityimportsampler
fromsignalUtilityimportreconstructor
fromscipyimportsignalasscipySignal
fromcopyimportcopy
importsys
importnumpyasnp
importscipy.fftpackasscipyFFT
fromsignalUtilityimportsignalType
# Create three different types waveform
f1=functionGenerator()
s1=copy(f1.sinWaveformGenerate(frequency=100, noOfCycle=20, phase=0))
# Add gaussian noise to signal 1
s2=copy(f1.addNoise(s1,noiseType='gaussian', parameter1=0,parameter2=0.1))
# sample noisy signal, sampling frequency is 10 times of original signal about original signal
# this is s2 signal which we will be getting in real scenario, we assume that we have some idea
# (like it's frequency, if we don't have any previous information we can visualise the signal and try to extract some info) so we will be able to design filter.
adc=sampler()
Fs=10000
s3=copy(adc.sampleSignal(s2,samplingFrequency=Fs))
o1=oscilloscope(title='Signal Visualisation')
o1.addWaveform(s1,plotColor='C0', plotLabel='Original Signal')
o1.addWaveform(s2,plotColor='C1', plotLabel='Noisy signal')
o1.addWaveform(s3,plotType='discrete',plotColor='C2', plotLabel='sampled discrete signal')
#FFT calculation for Signal
signalLen=len(s3.time)
fftLen=(int)(pow(2,np.ceil(np.log2(signalLen))))
fftPoints=copy(scipyFFT.fft(s3.value,n=fftLen))
freq=np.linspace(0,Fs,fftLen,endpoint=False)
o2=oscilloscope(title="Frequency Response (FFT)",isFreqResponsePlotAlso='yes')
o2.addFrequencyResponse(freq,fftPoints,wc=Fs/2,freqView='linear',ampView='linear',label="signal FFT, analog freq vs amplitude")
# filters
lowPassFilterCutoff=200
fc=lowPassFilterCutoff/Fs
wc=fc*(2*np.pi)
nyquistFrequency=np.pi
wc_normalized=wc/nyquistFrequency
b,a=scipySignal.butter(4,wc_normalized,btype='low',output='ba',analog=False)
# plot frequency response of filter
w,h=scipySignal.freqz(b,a,worN=fftLen,whole=True)
w=copy(w*(Fs/(2*np.pi)))
o2.addFrequencyResponse(w,h,wc=lowPassFilterCutoff,frequencyResponseColor='C1',freqView='linear',ampView='log',label="filter response, analog freq vs amplitude in db")
# filtering
filteredFFTAmplitude=np.multiply(fftPoints,h)
ifftSignal=copy(np.real(scipyFFT.ifft(filteredFFTAmplitude ,fftLen)))
ifftSignal=copy(ifftSignal[np.arange(0,len(s3.time))])
filteredDiscreteSignal=signalType(time=s3.time,value=ifftSignal)
#o1.addWaveform(filteredDiscreteSignal,plotType='discrete',plotColor='C2', plotLabel='Filtered discrete signal')
# reconstruction
dac=reconstructor()
reconstructedFilteredSignal=copy(dac.reconstructSignal(filteredDiscreteSignal,connectFilter='yes',lowPassCutoff=2000))
o1.addWaveform(reconstructedFilteredSignal,plotType='continious',plotColor='C3', plotLabel='filtered signal')