from signalUtility import signalType # 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='log',label="signal FFT, analog freq vs amplitude") # filters
s_10 = copy(f_10.sinWaveformGenerate(frequency=10,noOfCycle=3)) s_100 = copy(f_100.sinWaveformGenerate(frequency=100,noOfCycle=30)) s_1000 = copy(f_1000.sinWaveformGenerate(frequency=1000,noOfCycle=300)) s_add = signalType() s_add.time = copy(s_10.time) s_add.value = copy(s_10.value + s_100.value +s_1000.value) s = copy(f_10.addNoise(s_add,noiseType='gaussian',parameter1=0, parameter2=0.3)) Fs = 10000 adc = sampler() s_sampled = adc.sampleSignal(s_add,samplingFrequency=Fs) o1 = oscilloscope(title="Fourier analysis ",isSubPlot='yes') o1.createSubplots(noOfRows=2,noOfColumns=2) o1.addToSubplot(s_add,addToRows=0,addToColumns=0,plotTitle='Signals', plotColor='C0') o1.addToSubplot(s_add,addToRows=0,addToColumns=1,plotTitle='Sampled Signals', plotType='discrete', plotColor='C0') #FFT calculation signalLen = len(s_sampled.time) fftLen=(int)(pow(2,np.ceil(np.log2(signalLen)))) fftPoints = copy(scipyFFT.fft(s_sampled.value,n=fftLen)) freq = np.linspace(0,Fs,fftLen,endpoint=True); fftSignal = signalType(freq[np.arange(0,fftLen/2,1,dtype=int)],20*np.log10(abs(fftPoints[np.arange(0,fftLen/2,1,dtype=int)]))) o1.addToSubplot(fftSignal,addToRows=1,addToColumns=0,plotTitle='signal FFT', plotColor='C0',xlabel="frequency(Hz)",ylabel="FFT values")
""" from signalUtility import functionGenerator from signalUtility import oscilloscope from signalUtility import sampler from signalUtility import reconstructor from copy import copy import sys # Task 1, create three different types waveform f1 = functionGenerator() s1= copy(f1.sinWaveformGenerate(frequency = 100, noOfCycle=10, phase = 0)) s2= copy(f1.pulseWaveformGenerate(frequency=50,phase=180,noOfCycle=5,dutyCycle=0.5, offset=0, amplitude=0.5)) s3= copy(f1.triangularWaveformGenerate(frequency=50,phase=90,noOfCycle=10,dutyCycle=0.5, offset=0, amplitude=0.5)) o1=oscilloscope(title='Signal Visualisation') o1.addWaveform(s1,plotColor='C0', plotLabel='S1') o1.addWaveform(s2,plotColor='C1', plotLabel='S2') o1.addWaveform(s3,plotColor='C2', plotLabel='S3') # task 2, add noise to signal 1 s4 = copy(f1.addNoise(s1,noiseType='gaussian', parameter1=0,parameter2=0.1)) s5 = copy(f1.addNoise(s1,noiseType='uniform', parameter1=-0.1,parameter2=0.1)) o2=oscilloscope(title='Noisy Signal Visualisation') o2.addWaveform(s1,plotColor='C0', plotLabel='S1') o2.addWaveform(s4,plotColor='C1', plotLabel='gaussian noise added with signal') o2.addWaveform(s5,plotColor='C2', plotLabel='uniform noise added with signal') # Task 3, sampling and reconstruction
s_10 = copy(f_10.sinWaveformGenerate(frequency=10, noOfCycle=3)) s_100 = copy(f_100.sinWaveformGenerate(frequency=100, noOfCycle=30)) s_1000 = copy(f_1000.sinWaveformGenerate(frequency=1000, noOfCycle=300)) s_add = signalType() s_add.time = copy(s_10.time) s_add.value = copy(s_10.value + s_100.value + s_1000.value) s = copy( f_10.addNoise(s_add, noiseType='gaussian', parameter1=0, parameter2=0.3)) Fs = 10000 adc = sampler() s_sampled = adc.sampleSignal(s, samplingFrequency=Fs) o1 = oscilloscope(title="plots", isSubPlot='yes') o1.createSubplots(noOfRows=2, noOfColumns=2) o1.addToSubplot(s, addToRows=0, addToColumns=0, plotTitle="analog signal to be analysed") #FFT calculation for Signal signalLen = len(s_sampled.time) fftLen = (int)(pow(2, np.ceil(np.log2(signalLen)))) fftPoints = copy(scipyFFT.fft(s_sampled.value, n=fftLen)) freq = np.linspace(0, Fs, fftLen, endpoint=False) o2 = oscilloscope(title="Frequency Response (FFT)", isFreqResponsePlotAlso='yes')
btype='bandpass') w2, h2 = scipySignal.freqz(b2, a2) # filter3 fc31 = 900 fc32 = 1100 wc31 = (fc31 / Fs) * (2 * np.pi) wc32 = (fc32 / Fs) * (2 * np.pi) wc31_normalised = wc31 / nyquistFrequency wc32_normalised = wc32 / nyquistFrequency b3, a3 = scipySignal.butter(4, [wc31_normalised, wc32_normalised], btype='bandpass') w3, h3 = scipySignal.freqz(b3, a3) o3 = oscilloscope(title='filter response', isFreqResponsePlotAlso="yes") o3.addFrequencyResponse(w1, h1, wc=wc11, frequencyResponseColor='C0', label='filter 1') o3.addFrequencyResponse(w2, h2, wc=[wc21, wc22], frequencyResponseColor='C1', label='filter2') o3.addFrequencyResponse(w3, h3, wc=[wc31, wc32], frequencyResponseColor='C2', label='filter3')
import sys import numpy as np # 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() samplingFrequency = 1000 s3 = copy(adc.sampleSignal(s2, samplingFrequency=samplingFrequency)) 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') #****************************************************************************** """ Digital filter design: we will use 4th order butter worth low pass filter with cutoff at 200hz """ # filter design lowPassFilterCutoff = 200