Reputation: 5746
I have a square (pulse) signal which has a frequency fq
, and is played between t0
and tf
. To create the timeline, I simply do:
import numpy as np
t0 = 0
tf = 200
fq = 20
timeline = np.round(np.arange(t0, tf, 1/fq*1000), 3)
A frequency of 20 Hz means a pulse is delivered every 50 ms. The timeline outputted is:
timeline
Out[284]: array([ 0., 50., 100., 150.])
Now, I would like the same think, but with a burst of n_pulse
at frequency fq_burst
repeated at the frequency fq
.
Example: A burst of 4 pulses at 500 Hz repeated at 20 Hz should give me the timeline:
timeline
Out[282]:
array([ 0., 2., 4., 6., 50., 52., 54., 56., 100., 102., 104.,
106., 150., 152., 154., 156.])
My current solution works but is far from being nice and optimal:
start_burst = np.round(np.arange(t0, tf, 1/fq*1000), 3)
timing = list()
for t in start_burst:
for n in range(n_pulse):
timing.append(t + n*(1/fq_burst*1000))
timeline = np.round(np.asarray(timing), 3)
Any suggestion on how to improve this timeline generation? I'm thinking of a way to create directly the values in a numpy array and then to reshape it, but I can't figure out how to do it.
Thanks!
Upvotes: 2
Views: 238
Reputation: 59681
A simple way:
import numpy as np
def burst_pulse(t0, tf, fq, n_pulse, fq_burst):
timeline = np.arange(t0, tf, 1 / fq * 1000)
burst = np.arange(n_pulse) / fq_burst * 1000
timeline_burst = (timeline[:, np.newaxis] + burst[np.newaxis, :]).reshape((-1,))
return np.round(timeline_burst, 3)
print(burst_pulse(0, 200, 20, 4, 500))
Output:
[ 0. 2. 4. 6. 50. 52. 54. 56. 100. 102. 104. 106.
150. 152. 154. 156.]
Upvotes: 2