Python scipy.signal 模块,square() 实例源码
我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用scipy.signal.square()。
def __init__(self, mgr, sampRate=128,
chans=[str(n)+'x' for n in np.power(2, np.arange(8))/2.0],
waveform='sinusoid', freq=1.0, mix='none', pollSize=2):
"""
Construct a new wave generator source.
Args:
sampRate: Floating point value of the initial sampling frequency.
chans: Tuple of strings containing the initial channel
configuration.
waveform: String describing the type of waveform to produce.
May be 'sinusoid' or 'sawtooth' or 'square'
freq: Base frequency. Each channel is a power-of-two
multiple of this frequency.
pollSize: Number of data samples collected during each poll.
Higher values result in better timing and marker
resolution but more cpu usage while higher values
typically use less cpu but worse timing results.
"""
self.waveform = mp.Value('I', 0)
self.freq = mp.Value('d', freq)
self.t0 = mp.Value('d', 0.0)
self.t0.value = 0.0
self.pollSize = pollSize
self.lock = mp.Lock()
Source.__init__(self, mgr=mgr, sampRate=sampRate, chans=chans,
configPanelClass=WaveGenConfigPanel)
self.setWaveform(waveform)
self.mixArr = mp.Array('d', self.getNChan()*self.getNChan())
self.mixMat = (np.frombuffer(self.mixArr.get_obj())
.reshape((-1,self.getNChan())))
self.setMix(mix)
def setWaveform(self, waveform):
"""Set the periodic waveform to generate.
Args:
waveform: String describing the type of waveform to produce.
May be 'sinusoid' or 'sawtooth' or 'square'
"""
waveform = waveform.lower()
with self.lock:
try:
# index into keys gives us an integer id
self.waveform.value = list(waveforms.keys()).index(waveform)
except ValueError:
raise ValueError('Invalid waveform %s.' % str(waveform))
def __call__(self, t):
return self.amp * signal.square(2 * np.pi * self.freq * t, duty=0.5)
def generate_chord(T=1,fs=44100,noise=True):
"""
Generate random major chord with amplitude 1
for duration T with sampling rate fs
Return chord and label (pitch class)
0 = Ab / G#
1 = A
2 = Bb / A#
3 = B
4 = C
5 = Db / C#
6 = D
7 = Eb / D#
8 = E
9 = F
10 = Gb / F#
11 = G
"""
t = np.linspace(0,T,T*fs)
n1 = np.random.randint(1+17,88-10)
chord_inversion = np.random.randint(1,6)
n3 = 0
n5 = 0
if chord_inversion == 1:
n3 = n1 + 4
n5 = n1 + 7
if chord_inversion == 2:
n3 = n1 + 7
n5 = n1 + 16
if chord_inversion == 3:
n3 = n1 + -8
n5 = n1 + 7
if chord_inversion == 4:
n3 = n1 + -8
n5 = n1 + -5
if chord_inversion == 5:
n3 = n1 + 4
n5 = n1 + -5
if chord_inversion == 6:
n3 = n1 + -8
n5 = n1 + -17
f1 = 2**((n1-49)/12.0)*440
f2 = 2**((n3-49)/12.0)*440
f3 = 2**((n5-49)/12.0)*440
phase1 = np.random.uniform(0, 2*np.pi, 1)
phase2 = np.random.uniform(0, 1)
phase3 = np.random.uniform(0, 1)
label = (n1 % 12) + 1
noise_var = np.random.uniform(0, 1, 1)
sig = signal.square(f1*2*np.pi*t + phase1) + signal.square(f2*2*np.pi*t + phase2) + signal.square(f3*2*np.pi*t + phase3)
if noise:
sig = sig + noise_var*np.random.randn(int(T*fs))
return sig/3, label-1