dsp/lib/sweeps.py

56 lines
1.6 KiB
Python
Raw Normal View History

2015-10-18 23:06:39 -07:00
from . import tau
import numpy as np
2017-09-21 04:04:22 -07:00
2015-10-18 23:06:39 -07:00
def sweep(amp, length, begin=20, end=20480, method='linear'):
method = method or 'linear'
xs = np.arange(length)/length
if method in ('linear', 'quadratic', 'logarithmic', 'hyperbolic'):
ys = amp*sig.chirp(xs, begin, 1, end, method=method)
elif method is 'sinesweep':
# because xs ranges from 0:1, length is 1 and has been simplified out
2017-09-21 04:04:22 -07:00
domain = np.log((tau * end)/(tau * begin))
ys = amp*np.sin((tau * begin)/domain*(np.exp(xs*domain) - 1))
2015-10-18 23:06:39 -07:00
return ys
2017-09-21 04:04:22 -07:00
2015-10-18 23:06:39 -07:00
def tsp(N, m=0.5):
"""
OATSP(Optimized Aoshima's Time-Stretched Pulse) generator
x = tsp( N, m )
N : length of the time-stretched pulse
m : ratio of the swept sine (0 < m < 1)
Author(s): Seigo UTO 8-23-95
Reference:
Yoiti SUZUKI, Futoshi ASANO, Hack-Yoon KIM and Toshio SONE,
"Considerations on the Design of Time-Stretched Pulses,"
Techical Report of IEICE, EA92-86(1992-12)
"""
# http://www.sound.sie.dendai.ac.jp/dsp/e-21.html
if m < 0 or m > 1:
2015-10-18 23:33:46 -07:00
raise Exception("what are you doinggg")
2015-10-18 23:06:39 -07:00
if N < 0:
raise Exception("The number of length must be the positive number")
2017-09-30 04:04:27 -07:00
NN = int(2**np.floor(np.log2(N))) # nearest
NN2 = NN // 2
M = int(np.round(NN2 * m))
2015-10-18 23:06:39 -07:00
2017-09-30 04:04:27 -07:00
nn2 = np.square(np.arange(NN2 + 1))
2015-10-18 23:06:39 -07:00
j = np.complex(0, 1)
2017-09-30 04:04:27 -07:00
H = np.exp(j * 4 * M * np.pi * nn2 / np.square(NN))
2015-11-10 04:04:41 -08:00
H2 = np.r_[H, np.conj(H[1:NN2][::-1])]
2015-10-18 23:06:39 -07:00
x = np.fft.ifft(H2)
2015-11-10 04:04:41 -08:00
x = np.r_[x[NN2 - M:NN + 1], x[0:NN2 - M + 1]]
2017-09-30 04:04:27 -07:00
x = np.r_[x.real, np.zeros(N - NN)]
2015-10-18 23:06:39 -07:00
return x