-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_aliased_hann.py
56 lines (45 loc) · 1.44 KB
/
time_aliased_hann.py
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
#!/usr/bin/ipython -pylab
# Copyright 2010, Bill Cox, all rights reserved. This program may be freely
# distributed under the terms of the GPL license, version 2.
from pylab import *
from scipy import *
sampleRate = 10000
def computeNoiseBandwidth(response):
total = 0.0
peak = -1e50
for i in range(sampleRate/2):
if response[i] > peak:
peak = response[i]
peak = peak*peak
for i in range(sampleRate/2):
total += response[i]*response[i]
return total/peak
def genResponse(freq):
signal=zeros(sampleRate*2)
for i in range(sampleRate*2):
signal[i] = sin(2*pi*freq*i/sampleRate)
window=zeros(sampleRate*2)
for i in range(sampleRate*2):
window[i] = (1 - cos(pi*i/sampleRate))/2
# Apply the window function to the signal
windowedSignal = window*signal
# Now do the overlap-add
fftIn = windowedSignal[:sampleRate] + windowedSignal[sampleRate:]
# Compute the FFT
fftOut = fft(fftIn)
return abs(fftOut[:sampleRate/2])
worstValue = -1e50
worstFreq = 0.0
for i in range(100):
freq = 1000.0 + i/100.0
response = genResponse(freq)
testFreq = 1.1*freq
if response[testFreq] > worstValue:
worstValue = response[testFreq]
worstFreq = freq
print "Worst freq = %f" % worstFreq
response = genResponse(worstFreq)
plot(20*log10(response[900:1100]))
response = genResponse(1000)
B = computeNoiseBandwidth(response)
print "B = %f" % B