Created
January 15, 2012 16:27
-
-
Save catchmrbharath/1616336 to your computer and use it in GitHub Desktop.
ass2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numpy import * | |
from scipy import * | |
from matplotlib.pyplot import * | |
from pylab import * | |
from numpy.fft import * | |
import scipy.signal as signal | |
def mfreqz(b,n,a=1): | |
""" function to plot the response of a filter""" | |
figure(n) | |
w,h = signal.freqz(b,a) | |
h_dB = 20 * log10 (abs(h)) | |
subplot(211) | |
plot(w/max(w),h_dB) | |
ylim(-80, 30) | |
ylabel('Magnitude (db)') | |
xlabel(r'Frequency normalized to 1') | |
title(r'Frequency response') | |
subplot(212) | |
h_Phase = unwrap(arctan2(imag(h),real(h))) | |
plot(w/max(w),h_Phase) | |
ylabel('Phase (radians)') | |
xlabel(r'Normalized Frequency (x$\pi$rad/sample)') | |
title(r'Phase response') | |
subplots_adjust(hspace=0.5) | |
def bitcheck(filt,bits): | |
"""function to convert the filter coefficients to a fixed bit coefficient""" | |
temp = array(filt*(2**bits),dtype='int32') | |
filt2 = array(temp/(2.0**bits),dtype='float') | |
mfreqz(filt2,bits) | |
def quantize(wave,bits): | |
"""quantizes the waveform to the number of bits""" | |
temp = array(wave*(2**(bits-1)),dtype='int32') | |
quantizedWave= array(temp/(2.0**(bits-1)),dtype='float') | |
return quantizedWave | |
def binary(coeffs,bits): | |
"""converts float values to its binary form . coeffs should not be greater than 1""" | |
tempCoeffs = array(coeffs*(2**bits-1),dtype='int32') | |
print tempCoeffs | |
bincoef = [] | |
binstr = lambda n: n>0 and binstr(n>>1)+str(n&1) or '' | |
for coef in tempCoeffs: | |
temp=coef | |
if(coef>=0): | |
tempstr=('0'+binstr(temp)) | |
else: | |
tempstr=('1'+binstr(abs(temp))) | |
bincoef.append(tempstr+'0'*(bits-len(tempstr))) | |
print bincoef | |
pass | |
filt = signal.remez(18,array([0.0,700,1150,2000]),[1.0,0],Hz=4000) | |
mfreqz(filt,0) | |
F = 4000 #sampling frequency | |
N = 8000 | |
wave = sin(2*pi*300*arange(N)/F)+sin(2*pi*1500*arange(N)/F) | |
filteredWave=signal.lfilter(filt,1,wave) | |
filtWavefft = fftshift(fft(filteredWave)) | |
figure(1) | |
subplot(211) | |
title("Filtered wave") | |
plot(range(200),filteredWave[:200]) | |
freq = linspace(-F/2,F/2,N) | |
subplot(212) | |
plot(freq,abs(filtWavefft),'k') | |
title("frequency response of the wave") | |
subplots_adjust(hspace=0.5) | |
quantizedWave = quantize(wave,8) | |
filteredWave=signal.lfilter(filt,1,wave) | |
figure(3) | |
subplot(211) | |
title("Filtered quantized wave") | |
plot(range(200),filteredWave[:200]) | |
freq = linspace(-F/2,F/2,N) | |
subplot(212) | |
plot(freq,abs(filtWavefft),'k') | |
title("frequency response of the filtered quantized wave") | |
subplots_adjust(hspace=0.5) | |
binary(filt,8) | |
show() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from numpy import * | |
from scipy import * | |
from matplotlib.pyplot import * | |
from pylab import * | |
from numpy.fft import * | |
import scipy.signal as signal | |
F = 4000 #sampling frequency | |
N = 8000 | |
sig1 = sin(2*pi*500*arange(N)/F) | |
figure(1) | |
title("plot of the sine wave of 500Hz") | |
plot(arange(50),sig1[:50],'k') | |
fsig1 = fftshift(fft(sig1)) | |
freq = linspace(-F/2,F/2,N) | |
figure(2) | |
title("fft of sine wave of 500Hz") | |
plot(freq,abs(fsig1)) | |
sig2 = sin(2*pi*500*arange(N)/F)+0.5*randn(N) | |
figure(3) | |
title("plot of the sine wave of 500Hz with gaussian noise") | |
plot(arange(50),sig2[:50],'k') | |
fsig2 = fftshift(fft(sig2)) | |
freq = linspace(-F/2,F/2,N) | |
figure(4) | |
title("fft of sine wave of 500Hz with gaussian noise") | |
plot(freq,abs(fsig2)) | |
show() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment