dsp/lib/sweeps.py

55 lines
1.6 KiB
Python
Raw Permalink Normal View History

2018-02-16 04:04:24 -08:00
from . import tau, ceil2
2015-10-18 23:06:39 -07:00
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")
2018-02-16 04:04:24 -08:00
NN = ceil2(N)
2017-09-30 04:04:27 -07:00
NN2 = NN // 2
M = int(np.round(NN2 * m))
2015-10-18 23:06:39 -07:00
2018-02-16 04:04:24 -08:00
# this has been tweaked to prevent overflow:
s = np.square(np.arange(NN2 + 1) / NN)
2015-10-18 23:06:39 -07:00
2019-01-10 04:04:20 -08:00
H = np.exp(4j * M * np.pi * s)
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