-
Notifications
You must be signed in to change notification settings - Fork 0
/
sigproc_f.py
1441 lines (1255 loc) · 57.7 KB
/
sigproc_f.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#import math, random, numpy, itertools, rdvdif3, operator, functools, fractions, userinput, hilbert, peak
import math, random, numpy, itertools, rdvdif3, operator, functools, fractions, hilbert, peak
# magic! (ylppa = apply spelled backwards :D)
ylppa = lambda *args, **kwargs: lambda f: f(*args, **kwargs)
#D = userinput.debug
D = lambda *args: None
class Spectrum(numpy.ndarray):
def __new__(cls, input_array, **kwargs):
# Input array is an already formed ndarray instance
# We first cast to be our class type
obj = numpy.asarray(input_array).view(cls)
# add the new attributes to the created instance
obj.__dict__ = kwargs
return obj
def __array_finalize__(self, obj):
# see InfoArray.__array_finalize__ for comments
if obj is not None:
self.__dict__ = getattr(obj, '__dict__', dict())
# freqs = [f0, f1, f2, ...., fN]
# assume: freqs are increasing such that f(N+1) >= f(N)
# return:
# ([f0', f1', f2', ..., fN'], "UNIT")
# with unit "kHz", "MHz", "GHz", whatever
multipliers = ["", "k", "M", "G", "T", "P", "E", "Z", "Y", "y", "z", "a", "f", "p", "n", "u", "m"]
# 0 3 6 9 12 15 18 21 24 -24 -21 -18 -15 -12 -9 -6 -3
def rescale(freqs, unit):
decades = int(math.log10(abs(freqs[-1] - freqs[0])) // 3)
return (math.pow(10, decades*3), "{0}{1}".format(multipliers[decades], unit))
def idx_peakert(x, threshold=0.9, n=3):
indices = sorted(peak.indexes(x, threshold), key=lambda p: x[p])
return indices[:(n if len(indices)>n else None)]
#
random.seed()
def summary(x, descr):
# define what and how to print
fmt = "{0}:{1:+12.10f}".format
props = [("min", numpy.min), ("max", numpy.max), ("avg", numpy.mean), ("std", numpy.std)]
print(descr,"/"," ".join(map(lambda pair: fmt(pair[0], pair[1](x)), props)))
def rdbeamform(fn):
# code for reading n heaps:
#a = numpy.fromfile(fn, dtype=numpy.int8, count=1*1024*128*2)
#b = a.astype(numpy.float32)
#c = b.view(numpy.complex64)
#d = c.reshape(1, 1024, 128).transpose((0, 2, 1))
with open(fn) as infile:
# read one heap
heapnum = 0
while True:
try:
curHeap = numpy.fromfile(infile, dtype=numpy.int8, count=1024*128*2).astype(numpy.float32).view(numpy.complex64).reshape((1024,128)).T #transpose((1,0))
for s in range(curHeap.shape[0]):
yield curHeap[s][::-1]
heapnum = heapnum + 1
except Exception as E:
print("Caught exception reading heap #",heapnum," - ",E)
raise StopIteration
def rdbeamform_raw(fn):
# code for reading n heaps:
#a = numpy.fromfile(fn, dtype=numpy.int8, count=1*1024*128*2)
#b = a.astype(numpy.float32)
#c = b.view(numpy.complex64)
#d = c.reshape(1, 1024, 128).transpose((0, 2, 1))
with open(fn) as infile:
# read one heap
heapnum = 0
while True:
try:
curHeap = numpy.fromfile(infile, dtype=numpy.int8, count=1024*128*2).astype(numpy.float32).view(numpy.complex64).reshape((1024,128)).T #transpose((1,0))
for s in range(curHeap.shape[0]):
yield curHeap[s]
heapnum = heapnum + 1
except Exception as E:
print("Caught exception reading heap #",heapnum," - ",E)
raise StopIteration
def rdbeamform_meerkat(fn, nspec=128, start_spectrum=0):
# meerkat beamform is in HDF5
import h5py, pickle
with h5py.File(fn, 'r') as infile:
data = infile['Data']['bf_raw']
tel_state = infile['TelescopeState']
bandwidth = pickle.loads(tel_state.attrs['i0_bandwidth'])
centre_freq = pickle.loads(tel_state.attrs['sdp_l0_center_freq'])
dc_freq = centre_freq - bandwidth/2.0
print("rdbeamform_meerkat: file=",fn," start_spectrum=",start_spectrum," nspec=",nspec, " data.shape=",data.shape)
while True:
try:
yield Spectrum( data[:,start_spectrum:start_spectrum+nspec,:]
.astype(numpy.float32)\
.view(numpy.complex64)\
.squeeze()\
.T,\
DCEdge=0, DCFrequency=dc_freq, BW=bandwidth)
start_spectrum += nspec
except Exception as E:
print("Caught exception reading spectrum #",start_spectrum," - ",E)
raise StopIteration
def rdbeamform_heap(fn, nheap=1, start_spectrum=0):
# code for reading n heaps:
#a = numpy.fromfile(fn, dtype=numpy.int8, count=1*1024*128*2)
#b = a.astype(numpy.float32)
#c = b.view(numpy.complex64)
#d = c.reshape(1, 1024, 128).transpose((0, 2, 1))
with open(fn) as infile:
# compute start of heap
heapnum = start_spectrum // 128
start_spectrum = start_spectrum - (heapnum * 128)
infile.seek(heapnum * 128 * 2048)
print("rdbeamform_heap: file=",fn," start_spectrum=",start_spectrum," heap=",heapnum," seek to ",heapnum * 128 * 2048, " nheap=",nheap)
while True:
try:
yield Spectrum(numpy.fromfile(infile, dtype=numpy.int8, count=nheap*1024*128*2)\
.astype(numpy.float32)\
.view(numpy.complex64)\
.reshape((nheap, 1024,128))\
.transpose((0, 2, 1))\
.reshape((nheap * 128, 1024)),
DCEdge=(1700.49e6 + 200e6), BW=-400e6)[start_spectrum:]
start_spectrum = 0
heapnum = heapnum + nheap
except Exception as E:
print("Caught exception reading heap #",heapnum," - ",E)
raise StopIteration
def averager(quant, src):
data_sum = quant_sum = None #numpy.ndarray(0)
for s in src:
data_sum = numpy.vstack([data_sum, s]) if data_sum is not None else s
quant_sum = quant_sum + quant(s) if quant_sum is not None else quant(s)
yield quant_sum/data_sum.shape[0]
yield quant(numpy.average(data_sum, axis=1))
def quantizer(quant, src):
for s in src:
yield quant(s)
# assume mods = [ (idx, value), .... ]
def replacer(mods, src):
(indices, values) = zip(*mods)
indices = list(indices)
values = numpy.array( list(values) )
for s in src:
s[indices] = values
yield s
def replacer_h(mods, src):
(indices, values) = zip(*mods)
indices = list(indices)
values = numpy.array( list(values) )
for s in src:
s[:,indices] = values
yield s
def auto_comb(n, src):
for spectrum in src:
# generate spikes
spike_idx = numpy.linspace(len(spectrum)/n, len(spectrum), n, endpoint=False, dtype=numpy.uint32)
m = numpy.max(spectrum)
spectrum[ spike_idx ] = m*(1.5 + numpy.sin( numpy.linspace(0, math.pi, len(spike_idx)) ))
yield spectrum
def take(n, src):
while n>0:
yield next(src)
n = n-1
def testbf(src):
import matplotlib.pyplot as plt
f, plots = plt.subplots(nrows=2, ncols=1)
for (i,s) in enumerate(src):
plots[i % 2].plot( s )
plt.show()
# freq is assumed to be in Hz
PI2 = 2*math.pi
SIN = math.sin
def sinewave(freq, phase=0.0, amplitude=1.0):
period = 1.0/freq
def gen(ts):
# compute the value for the given timestamp (unit: seconds)
return amplitude * SIN( PI2 * ts/period + phase )
return gen
def gaussian_noise(amplitude=1.0, mean=0.0):
def gen(ts):
return amplitude * numpy.random.randn() + mean
return gen
def sample(*signals):
def gen(ts):
return map(ylppa(ts), signals)
return gen
def add(*signals):
sampler = sample(*signals)
def gen(ts):
return sum(sampler(ts))
#return sum(map(ylppa(ts), signals))
return gen
def mix(*signals):
sampler = sample(*signals)
def gen(ts):
return functools.reduce(operator.mul, sampler(ts))
#return reduce(operator.mul, map(ylppa(ts), signals))
return gen
def take(n, generator):
result = []
while n>0:
result.append( next(generator) )
n = n-1
return result
# SNR is the ratio of the sine wave compared to the mean (guassian) noise
def sine_embedded_in_noise(freq, SNR=0.001):
return add(gaussian_noise(amplitude=1.0), sinewave(freq, amplitude=SNR))
def sines_embedded_in_noise(freqs, SNR=0.001):
return add(gaussian_noise(amplitude=1.0), *map(lambda f: sinewave(f, amplitude=SNR), freqs))
def mk(snr, freq=6, sr=100):
s = list(map(sine_embedded_in_noise(freq, SNR=snr), numpy.linspace(0,1,sr)))
return (s, numpy.fft.fft(s))
def mk2(line, lo, sr=100, snr=0.01, nseconds=1):
s = list(map(mix(sine_embedded_in_noise(line, SNR=snr), sinewave(lo)), numpy.linspace(0,nseconds*sr,sr)))
return (s, numpy.fft.fft(s))
def d(freq=6, snr=4):
#import matplotlib
#matplotlib.use('MacOSX')
import matplotlib.pyplot as plt
(sr_mi, sr_ma) = int(freq/2), int(freq*3)
for i in range(sr_mi,sr_ma,max(int((sr_ma-sr_mi)/20), 1)):
plt.figure().clear()
(z, ftz) = mk(snr, freq=freq, sr=i)
plt.plot(z); plt.plot( numpy.abs(ftz) ); plt.title("SR={0} F={1}".format(i, freq)); plt.show()
# take a time series and do digital downconversion
def ddc1(lo, bw, *signals):
import scipy.signal as SIGNAL
import matplotlib.pyplot as plt
# mix with cosine [should be complex?]
mixed = mix(sinewave(lo, phase=math.pi/2), *signals)
# we need a lowpass filter
sr = 8000
n_tap = 128
h = SIGNAL.firwin(n_tap, bw / sr)
series = list(map(mixed, numpy.linspace(0, 1, sr)))
ft_series = numpy.fft.fft(series)
series_f = SIGNAL.lfilter(h, 1, series)
ft_series_f = numpy.fft.fft(series_f)
f, plots = plt.subplots(nrows=4, ncols=1)
plots[0].plot( series )
plots[1].plot( numpy.abs(ft_series[:sr//2]) )
plots[2].plot( series_f )
plots[3].plot( numpy.abs(ft_series_f[:int(2*bw)]) )
plt.show()
# courtesy https://gist.github.com/endolith/114336
def gcd(*numbers):
"""Return the greatest common divisor of the given integers"""
return functools.reduce(fractions.gcd, numbers)
# Least common multiple is not in standard libraries? It's in gmpy, but this is simple enough:
def lcm(*numbers):
"""Return lowest common multiple."""
def lcm(a, b):
return (a * b) // gcd(a, b)
return functools.reduce(lcm, numbers)
# lcm for Fractions
import fractions
lcm_f_impl = lambda a, b: (a * b) / fractions.gcd(a, b)
def lcm_f(*numbers):
return functools.reduce(lcm_f_impl, map(fractions.Fraction, numbers))
# do samplerate conversion; sr_in, sr_out integer amount of samples / second
# need to find L, M such that L * sr_in / M = sr_out
# L = lcm(sr_in, sr_out) / sr_in
# M = lcm(sr_in, sr_out) / sr_out
def ddc2(lo, bw, sr_in, sr_out, *signals):
import scipy.signal as SIGNAL
import matplotlib.pyplot as plt
# Get L, M
mult = lcm(sr_in, sr_out)
L = mult // sr_in
M = mult // sr_out
print("DDC/sr_in={0} sr_out={1}, using sr_in * {2} / {3} = sr_out".format(sr_in, sr_out, L, M))
# mix with cosine [should be complex?]
mixed = mix(sinewave(lo, phase=math.pi/2), *signals)
# we need a lowpass filter
n_tap = 128
h = SIGNAL.firwin(n_tap, bw / sr_in)
series = list(map(mixed, numpy.linspace(0, 1, sr_in)))
ft_series = numpy.fft.fft(series)
# upsample
up = numpy.zeros(len(series)*L)
up[::L] = series
# 'kay. Now filter that upsampled series
series_f = SIGNAL.lfilter(h, 1, up)
ft_series_f = numpy.fft.fft(series_f)
# and downsample
down = series_f[::M]
ft_down = numpy.fft.fft(down)
f, plots = plt.subplots(nrows=6, ncols=1)
plots[0].plot( series )
plots[1].plot( numpy.abs(ft_series[:sr_in//2]) )
plots[2].plot( series_f )
plots[3].plot( numpy.abs(ft_series_f[:int(2*bw)]) )
plots[4].plot( down )
plots[5].plot( numpy.abs(ft_down[:int(2*bw)]) )
plt.show()
#peaks = lambda cutoff: lambda series: numpy.where(numpy.abs(series)>(cutoff*numpy.amax(numpy.abs(series))))[0]
#peaks = lambda nsd: lambda series: numpy.where(numpy.abs(series)>(numpy.mean(numpy.absseries)+nsd*numpy.std(numpy.abs(series))))[0]
def peaks_sd(nsd):
def doit(series):
a = numpy.abs(series)
return numpy.where(a>(numpy.mean(a)+nsd*numpy.std(a)))[0]
return doit
def peaks_peak(cutoff):
def doit(series):
a = numpy.abs(series)
return numpy.where(a>=(cutoff*numpy.max(a)))[0]
return doit
def ddc3(lo, sr_in, sr_out, *signals):
import scipy.signal as SIGNAL
import matplotlib.pyplot as plt
# Get L, M
mult = lcm(sr_in, sr_out)
L = mult // sr_in
M = mult // sr_out
print("DDC/sr_in={0} sr_out={1}, using sr_in * {2} / {3} = sr_out".format(sr_in, sr_out, L, M))
# mix with cosine [should be complex?]
mixed = mix(sinewave(lo, phase=math.pi/2), *signals)
# we need a lowpass filter - take sr_out / 2 as Nyquist limited band
n_tap = 128
h = SIGNAL.firwin(n_tap, sr_out/2.0/sr_in) #bw / sr_in)
series = list(map(mixed, numpy.linspace(0, 1, sr_in)))
ft_series = numpy.fft.fft(series)
# upsample
up = numpy.zeros(len(series)*L)
up[::L] = series
# 'kay. Now filter that upsampled series
series_f = SIGNAL.lfilter(h, 1, up)
ft_series_f = numpy.fft.fft(series_f)
# and downsample
down = series_f[::M]
ft_down = numpy.fft.fft(down)
all_peaks = list(map(peaks_peak(0.8), [ft_series, ft_series_f[:sr_in//2], ft_down[:sr_out//2]]))
print("peaks in FFT of mixed signal:", all_peaks[0])
print("peaks in FFT of upsampled+filtered mixed signal:",all_peaks[1]," +LO=",all_peaks[1]+lo)
print("peaks in FFT of downsampled signal:",all_peaks[2]," +LO=",all_peaks[2]+lo)
f, plots = plt.subplots(nrows=6, ncols=1)
plots[0].plot( series )
plots[1].plot( numpy.abs(ft_series[:sr_in//2]) )
# series_f is the upsampled-by-L-and-filtered signal
plots[2].plot( series_f )
plots[3].plot( numpy.abs(ft_series_f[:(sr_in * L)//2]) )
# downsampled signal has sr_out sample rate so after fft only 1/2 of the points are useful
plots[4].plot( down )
plots[5].plot( numpy.abs(ft_down[:sr_out//2]) )
plt.show()
################################################################################################################
#
# A working digital downconverter!
#
# lo = desired zero frequency
# sr_in, sr_out = sample rate of the input and output signals in samples/second
# dt = delta t = duration in seconds
#
#################################################################################################################
def ddc4(lo, sr_in, sr_out, dt, *signals):
import scipy.signal as SIGNAL
import matplotlib.pyplot as plt
# Get L, M
mult = lcm(sr_in, sr_out)
L = mult // sr_in
M = mult // sr_out
print("DDC/sr_in={0} sr_out={1}, using sr_in * {2} / {3} = sr_out".format(sr_in, sr_out, L, M))
# mix with cosine [should be complex?]
mixed = mix(sinewave(lo, phase=math.pi/2), *signals)
# we need a lowpass filter - take sr_out / 2 as Nyquist limited band
n_tap = 128
h = SIGNAL.firwin(n_tap, sr_out/1.0/sr_in)
# how many seconds?
series = list(map(mixed, numpy.arange(0, dt, 1.0/sr_in)))
ft_series = numpy.fft.fft(series)
# upsample
up = numpy.zeros(len(series)*L)
up[::L] = series
# 'kay. Now filter that upsampled series
series_f = SIGNAL.lfilter(h, 1, up)
ft_series_f = numpy.fft.fft(series_f)
# and downsample
down = series_f[::M]
ft_down = numpy.fft.fft(down)
# Analysis + plot part
# figure out which bits of the FFT to use.
# if we have more samples than the actual sample rate of the signal no point in showing > n/2 points
n_series = min(sr_in//2, len(series)//2)
n_series_f = min(len(series_f)//2, (sr_in * L)//2)
n_down = min(sr_out//2, int(len(up)/M/2))
all_peaks = list(map(peaks_peak(0.8), [ft_series[:n_series], ft_series_f[:n_series_f], ft_down[:n_down]]))
print("peaks in FFT of mixed signal:", all_peaks[0])
print("peaks in FFT of upsampled+filtered mixed signal:",all_peaks[1]," +LO=",all_peaks[1]/dt+lo)
print("peaks in FFT of downsampled signal:",all_peaks[2]," +LO=",all_peaks[2]/dt+lo)
f, plots = plt.subplots(nrows=6, ncols=1)
plots[0].plot( series )
plots[1].plot( numpy.abs(ft_series[:n_series]) )
# series_f is the upsampled-by-L-and-filtered signal
plots[2].plot( series_f )
plots[3].plot( numpy.abs(ft_series_f[:n_series_f]) )
# downsampled signal has sr_out sample rate so after fft only 1/2 of the points are useful
plots[4].plot( down )
plots[5].plot( numpy.abs(ft_down[:n_down]) )
plt.show()
# samples are assumed to be taken at sample rate sr_in
def ddc5(lo, sr_in, sr_out, samples):
import scipy.signal as SIGNAL
import matplotlib.pyplot as plt
# Get L, M
mult = lcm(sr_in, sr_out)
L = mult // sr_in
M = mult // sr_out
print("DDC/sr_in={0} sr_out={1}, using sr_in * {2} / {3} = sr_out".format(sr_in, sr_out, L, M))
# generate cosine to mix with
mixed = samples * numpy.cos( 2 * math.pi * lo * numpy.arange(len(samples))*1.0/sr_in )
# we need a lowpass filter - take sr_out / 2 as Nyquist limited band
n_tap = 128
h = SIGNAL.firwin(n_tap, sr_out/1.0/sr_in)
# how many seconds?
series = mixed #list(map(mixed, numpy.arange(0, dt, 1.0/sr_in)))
ft_series = numpy.fft.fft(series)
# upsample
up = numpy.zeros(len(series)*L)
up[::L] = series
# 'kay. Now filter that upsampled series
series_f = SIGNAL.lfilter(h, 1, up)
ft_series_f = numpy.fft.fft(series_f)
# and downsample
down = series_f[::M]
ft_down = numpy.fft.fft(down)
# Analysis + plot part
# figure out which bits of the FFT to use.
# if we have more samples than the actual sample rate of the signal no point in showing > n/2 points
dt = float(len(series))/sr_in
n_series = min(sr_in//2, len(series)//2)
n_series_f = min(len(series_f)//2, (sr_in * L)//2)
n_down = min(sr_out//2, int(len(up)/M/2))
all_peaks = list(map(peaks_peak(0.8), [ft_series[:n_series], ft_series_f[:n_series_f], ft_down[:n_down]]))
print("peaks in FFT of mixed signal:", all_peaks[0])
print("peaks in FFT of upsampled+filtered mixed signal:",all_peaks[1]," +LO=",all_peaks[1]/dt+lo)
print("peaks in FFT of downsampled signal:",all_peaks[2]," +LO=",all_peaks[2]/dt+lo)
f, plots = plt.subplots(nrows=6, ncols=1)
plots[0].plot( series )
plots[1].plot( numpy.abs(ft_series[:n_series]) )
# series_f is the upsampled-by-L-and-filtered signal
plots[2].plot( series_f )
plots[3].plot( numpy.abs(ft_series_f[:n_series_f]) )
# downsampled signal has sr_out sample rate so after fft only 1/2 of the points are useful
plots[4].plot( down )
plots[5].plot( numpy.abs(ft_down[:n_down]) )
#plt.show()
# DBBC giving upper + lower sidebands around lo
# sr_in/sr_out will be made rationals/fractions
def dbbc(lo, sr_in, sr_out, samples):
import scipy.signal as SIGNAL
import scipy.fftpack as FFT
import matplotlib.pyplot as plt
# make sure sr_in/sr_out are Fractions
sr_in, sr_out = list(map(fractions.Fraction, [sr_in, sr_out]))
# Get L, M
mult = lcm_f(sr_in, sr_out)
L = mult / sr_in
M = mult / sr_out
# Assert that both L and M are integer, not fractions.
# prevent ludicrous ratios - if L>>10 that would mean
# that, realistically, L and M are not very compatible
# [800 and 64 should give 25 and 2 which is 'acceptable']
if not (L.denominator==1 and M.denominator==1 and L<=10 and M<=200):
raise RuntimeError(("DBBC/ sample rates sr_in={0}, sr_out={1} cannot be transformed 'simply' "+
"through upsample by L, downsample by M; L={2} M={3}").format(sr_in, sr_out, L, M))
print("DBBC/sr_in={0} sr_out={1}, using sr_in * {2} / {3} = sr_out".format(sr_in, sr_out, L, M))
print(" LO=",lo)
# Make sure that L, M are back to normal integers
L = L.numerator
M = M.numerator
# make sure samples is the real part of our complex
D("making timeseries real ... n=",len(samples))
samples = numpy.array(numpy.real(samples), dtype=numpy.complex64)
# multiply by complex cosine exp( -j * 2 * pi * f * t)
D("mixing ... samples.dtype=",samples.dtype)
mixed = samples * numpy.exp( -2j * numpy.pi * lo * (numpy.arange(len(samples), dtype=numpy.float64)/numpy.float64(sr_in)) )
D(" len=",len(mixed)," dtype=",mixed.dtype)
#down = SIGNAL.resample_poly(mixed, L, M, window=('kaiser', 6.76))
#coeffs = SIGNAL.firwin(129, float(sr_out/sr_in)/2, window=userinput.window)
#D("resampling *",L,"/",M," [window=",userinput.window,"]")
down = SIGNAL.resample_poly(mixed, L, M, window=('kaiser', 14))
#down = SIGNAL.resample_poly(mixed, L, M, window=userinput.window)
D(" len=",len(down)," dtype=",down.dtype)
coeffs_r= hilbert.Hilbert45(501, 0.05, 0.05, 0.45, 0)
coeffs_i= hilbert.Hilbert45(501, 0.05, 0.05, 0.45, 1)
re = SIGNAL.lfilter(coeffs_r, 1, down.real)
im = SIGNAL.lfilter(coeffs_i, 1, down.imag)
summary(down.real, "Re(down)")
summary(down.imag, "Im(down)")
summary(re, "Re(hilb)")
summary(im, "Im(hilb)")
D("making USB ...")
usb = re - im #re + im
D("making LSB ...")
lsb = re + im #re - im
# About time to plot some things
f, plots = plt.subplots(nrows=4, ncols=1)
#f, plots = plt.subplots(nrows=3, ncols=1)
plt.title("dbbc/resample_poly")
D("plot samples ...")
plots[0].plot( samples[:min(len(samples), 8000)] )
D("plot down ... len=",len(down))
plots[1].plot( down[:min(len(down), 8000)] )
# The plotting part
ftsize = int(2*(userinput.ftsize//2))
nfft = int(len(lsb)//ftsize)
D("ftsize=",ftsize," nfft=",nfft)
# Do the USB
D("FFT usb/ len=",len(usb), " using ",ftsize*nfft)
spectra = numpy.fft.fft(usb[:(ftsize * nfft)].reshape((nfft, ftsize)), axis=1)
D(" spectra.shape=",spectra.shape)
usb_freq = numpy.sum(numpy.abs(spectra), axis=0)[:ftsize//2]
usb_pha = numpy.angle(numpy.mean(spectra, axis=0)[:ftsize//2], deg=True)
if lo<0:
x_axis_u = numpy.linspace(lo+(sr_out/2), lo, ftsize//2)
else:
x_axis_u = numpy.linspace(lo, (lo+sr_out/2), ftsize//2)
(u_scale, u_unit) = rescale(x_axis_u, "Hz")
#plots[2].set_xlim(x_axis_u[0]/u_scale, x_axis_u[-1]/u_scale)
plots[2].set_xlim( (lo - sr_out/2 - sr_out/10)/u_scale, (lo + sr_out/2 + sr_out/10)/u_scale )
plots[2].plot( x_axis_u/u_scale, usb_freq, 'r', alpha=0.5 )
plots[3].plot( x_axis_u/u_scale, usb_pha , 'r.', alpha=0.5 )
plots[2].set_xlabel(u_unit)
#plots[2].set_title("FFT/USB")
plots[2].set_title("FFT")
# Repeat for LSB
D("FFT lsb/ len=",len(lsb), " using ",ftsize*nfft)
spectra = numpy.fft.fft(lsb[:(ftsize * nfft)].reshape((nfft, ftsize)), axis=1)
lsb_freq = numpy.sum(numpy.abs(spectra), axis=0)[:ftsize//2]
lsb_pha = numpy.angle(numpy.mean(spectra, axis=0)[:ftsize//2], deg=True)
if lo<0:
x_axis_l = numpy.linspace(lo, lo-(sr_out/2), ftsize//2)
else:
x_axis_l = numpy.linspace(lo-(sr_out/2), lo, ftsize//2)
(l_scale, l_unit) = rescale(x_axis_l, "Hz")
#plots[3].set_xlim(x_axis_l[0]/l_scale, x_axis_l[-1]/l_scale)
#plots[3].plot( x_axis_l/l_scale, lsb_freq, 'g', alpha=0.5 )
plots[2].plot( x_axis_l/u_scale, lsb_freq, 'g', alpha=0.5 )
plots[3].plot( x_axis_l/u_scale, lsb_pha , 'g.', alpha=0.5 )
#plots[3].set_xlabel(l_unit)
#plots[3].set_title("FFT/LSB")
# Do loox0ring for peaks in USB/LSB
limit = userinput.peak_threshold
plots[2].axhline( limit*numpy.max(usb_freq) )
for p in idx_peakert(usb_freq, limit):
print("Found peak in USB: f={0:.8f}Hz [bin={1}]".format( x_axis_u[p], p))
plots[2].plot( x_axis_u[p]/u_scale, usb_freq[p], marker='v', color='r')
#plots[3].axhline( limit*numpy.max(lsb_freq) )
for p in idx_peakert(lsb_freq, limit):
print("Found peak in LSB: f={0:.8f}Hz [bin={1}]".format( x_axis_l[p], p))
#plots[3].plot( x_axis_l[p]/l_scale, lsb_freq[p], marker='v', color='g')
plots[2].plot( x_axis_l[p]/l_scale, lsb_freq[p], marker='v', color='g')
return (lsb, usb)
def dbbc_oldstyle(lo, sr_in, sr_out, samples):
import scipy.signal as SIGNAL
import scipy.fftpack as FFT
import matplotlib.pyplot as plt
# make sure sr_in/sr_out are Fractions
sr_in, sr_out = list(map(fractions.Fraction, [sr_in, sr_out]))
# Get L, M
mult = lcm_f(sr_in, sr_out)
L = mult / sr_in
M = mult / sr_out
# Assert that both L and M are integer, not fractions.
# prevent ludicrous ratios - if L>>10 that would mean
# that, realistically, L and M are not very compatible
# [800 and 64 should give 25 and 2 which is 'acceptable']
if not (L.denominator==1 and M.denominator==1 and L<=10 and M<=200):
raise RuntimeError(("DBBC/ sample rates sr_in={0}, sr_out={1} cannot be transformed 'simply' "+
"through upsample by L, downsample by M; L={2} M={3}").format(sr_in, sr_out, L, M))
print("DBBC/sr_in={0} sr_out={1}, using sr_in * {2} / {3} = sr_out".format(sr_in, sr_out, L, M))
print(" LO=",lo)
# Make sure that L, M are back to normal integers
L = L.numerator
M = M.numerator
# make sure samples is the real part of our complex
D("making timeseries real ... n=",len(samples))
samples = numpy.array(numpy.real(samples), dtype=numpy.complex64)
mixed = numpy.zeros(len(samples) * L, dtype=numpy.complex128)
# multiply by complex cosine exp( -j * 2 * pi * f * t)
D("mixing ... samples.dtype=",samples.dtype)
mixed[::L] = samples * numpy.exp( -2j * numpy.pi * lo * (numpy.arange(len(samples), dtype=numpy.float64)/numpy.float64(sr_in)) )
#mixed = samples * numpy.exp( -2j * numpy.pi * lo * (numpy.arange(len(samples), dtype=numpy.float64)/numpy.float64(sr_in)) )
D(" len=",len(mixed)," dtype=",mixed.dtype)
# windowing + FIR
# make it symmetric + odd
#win = SIGNAL.kaiser(51, beta=14)
win = 'hamming' #('kaiser', 14)
# the filter: cutoff at sr_out/sr_in (say 8MHz out of 400MHz -> cutoff = 8/400
#fir = SIGNAL.firwin(501, cutoff=sr_out*L, window=win, nyq=sr_in)
#fir = SIGNAL.firwin(501, cutoff=float(M*sr_out)/(L*sr_in), window=win, nyq=float(L*sr_in))
#fir = SIGNAL.firwin(501, cutoff=float(60*sr_out)/sr_in, nyq=float(sr_in))
#fir = SIGNAL.firwin(501, cutoff=0.00934, nyq=1.0)
fir = SIGNAL.firwin(501, cutoff=float(sr_out)/float(sr_in), window='hamming', nyq=1.0)
# mixed was already upsampled, now filter it
#zi = SIGNAL.lfilter_zi(fir, [1.0])
#(filtered,zf) = SIGNAL.lfilter(fir, [1.0], mixed, zi=zi)
filtered = SIGNAL.lfilter(fir, [1.0], mixed)
D("resampling *",L,"/",M," [window=",win,"]")
down = filtered[::M]
D(" len=",len(down)," dtype=",down.dtype)
#down = SIGNAL.resample_poly(mixed, L, M, window=('kaiser', 6.76))
#coeffs = SIGNAL.firwin(129, float(sr_out/sr_in)/2, window=userinput.window)
#D("resampling *",L,"/",M," [window=",userinput.window,"]")
#down = SIGNAL.resample_poly(mixed, L, M, window=userinput.window)
#D(" len=",len(down)," dtype=",down.dtype)
coeffs_r= hilbert.Hilbert45(501, 0.05, 0.05, 0.45, 0)
coeffs_i= hilbert.Hilbert45(501, 0.05, 0.05, 0.45, 1)
re = SIGNAL.lfilter(coeffs_r, 1, down.real)
im = SIGNAL.lfilter(coeffs_i, 1, down.imag)
summary(down.real, "Re(down)")
summary(down.imag, "Im(down)")
summary(re, "Re(hilb)")
summary(im, "Im(hilb)")
D("making USB ...")
usb = re - im #re + im
D("making LSB ...")
lsb = re + im #re - im
# About time to plot some things
f, plots = plt.subplots(nrows=4, ncols=1)
#f, plots = plt.subplots(nrows=3, ncols=1)
plt.title("dbbc/manual (i.e. [::L]=mixed, down=[::M])")
D("plot samples ...")
plots[0].plot( samples[:min(len(samples), 8000)] )
D("plot down ... len=",len(down))
plots[1].plot( down[:min(len(down), 8000)] )
# The plotting part
ftsize = int(2*(userinput.ftsize//2))
nfft = int(len(lsb)//ftsize)
D("ftsize=",ftsize," nfft=",nfft)
# Do the USB
D("FFT usb/ len=",len(usb), " using ",ftsize*nfft)
spectra = numpy.fft.fft(usb[:(ftsize * nfft)].reshape((nfft, ftsize)), axis=1)
usb_freq = numpy.sum(numpy.abs(spectra), axis=0)[:ftsize//2]
usb_pha = numpy.angle(numpy.mean(spectra, axis=0)[:ftsize//2], deg=True)
if lo<0:
x_axis_u = numpy.linspace(lo+(sr_out/2), lo, ftsize//2)
else:
x_axis_u = numpy.linspace(lo, (lo+sr_out/2), ftsize//2)
(u_scale, u_unit) = rescale(x_axis_u, "Hz")
#plots[2].set_xlim(x_axis_u[0]/u_scale, x_axis_u[-1]/u_scale)
plots[2].set_xlim( (lo - sr_out/2 - sr_out/10)/u_scale, (lo + sr_out/2 + sr_out/10)/u_scale )
plots[2].plot( x_axis_u/u_scale, usb_freq, 'r', alpha=0.5 )
plots[3].plot( x_axis_u/u_scale, usb_pha , 'r.', alpha=0.5 )
plots[2].set_xlabel(u_unit)
#plots[2].set_title("FFT/USB")
plots[2].set_title("FFT")
# Repeat for LSB
D("FFT lsb/ len=",len(lsb), " using ",ftsize*nfft)
spectra = numpy.fft.fft(lsb[:(ftsize * nfft)].reshape((nfft, ftsize)), axis=1)
lsb_freq = numpy.sum(numpy.abs(spectra), axis=0)[:ftsize//2]
lsb_pha = numpy.angle(numpy.mean(spectra, axis=0)[:ftsize//2], deg=True)
if lo<0:
x_axis_l = numpy.linspace(lo, lo-(sr_out/2), ftsize//2)
else:
x_axis_l = numpy.linspace(lo-(sr_out/2), lo, ftsize//2)
(l_scale, l_unit) = rescale(x_axis_l, "Hz")
#plots[3].set_xlim(x_axis_l[0]/l_scale, x_axis_l[-1]/l_scale)
#plots[3].plot( x_axis_l/l_scale, lsb_freq, 'g', alpha=0.5 )
plots[2].plot( x_axis_l/u_scale, lsb_freq, 'g', alpha=0.5 )
plots[3].plot( x_axis_l/u_scale, lsb_pha , 'g.', alpha=0.5 )
#plots[3].set_xlabel(l_unit)
#plots[3].set_title("FFT/LSB")
# Do loox0ring for peaks in USB/LSB
limit = userinput.peak_threshold
plots[2].axhline( limit*numpy.max(usb_freq) )
for p in idx_peakert(usb_freq, limit):
print("Found peak in USB: f={0:.8f}Hz [bin={1}]".format( x_axis_u[p], p))
plots[2].plot( x_axis_u[p]/u_scale, usb_freq[p], marker='v', color='r')
#plots[3].axhline( limit*numpy.max(lsb_freq) )
for p in idx_peakert(lsb_freq, limit):
print("Found peak in LSB: f={0:.8f}Hz [bin={1}]".format( x_axis_l[p], p))
#plots[3].plot( x_axis_l[p]/l_scale, lsb_freq[p], marker='v', color='g')
plots[2].plot( x_axis_l[p]/l_scale, lsb_freq[p], marker='v', color='g')
return (lsb, usb)
def requantize(ds):
ds = ((ds // (1.05*numpy.std(ds))).astype(numpy.int8).clip(-2, 1) + 2).reshape( len(ds)//4, 4 )
ds = numpy.sum((ds * [1, 4, 16, 64]).astype(numpy.int8), axis=1).astype(numpy.int8)
return ds
from ctypes import LittleEndianStructure, c_uint32, c_uint8
class VDIF_Header(LittleEndianStructure):
_pack_ = 1
_fields_ = [
# word 0
("seconds", c_uint32, 30),
("legacy", c_uint32, 1),
("invalid", c_uint32, 1),
# word 1
("frame_number", c_uint32, 24),
("epoch", c_uint32, 6),
("unassigned", c_uint32, 2),
# word 2
("frame_length_8bytes", c_uint32, 24),
("log2_channels", c_uint32, 5),
("version", c_uint32, 3),
# word 3
("station_id", c_uint32, 16),
("thread_id", c_uint32, 10),
("bits_per_sample_minus_1", c_uint32, 5),
("complex", c_uint32, 1),
# extended header
("word4", c_uint32),
("word5", c_uint32),
("word6", c_uint32),
("word7", c_uint32)
]
def wr_vdif_old(samples, fn):
# write integral multiple of 8 # of samples
n = (len(samples)//8) * 8
# simple single header: one thread w/ 1 channel @ 2bits/sample
hdr = VDIF_Header(frame_length_8bytes=(n + 32)//8,
bits_per_sample_minus1 = 1,
log2_channels = 0)
with open(fn, "wb") as output:
output.write( hdr )
output.write( requantize(samples[:n]) )
def wr_vdif(samples, fn):
# make sure we take a number of samples that is a multiple of 4 and 8
# (4 samples per byte (2 bits / sample), VDIF frame must be integer
# number of 8-byte words)
# let's requantize
quantized_packed = requantize( samples[ : (len(samples)//32)*32 ] )
# simple single header: one thread w/ 1 channel @ 2bits/sample
hdr = VDIF_Header(frame_length_8bytes=(len(quantized_packed) + 32)//8,
bits_per_sample_minus_1 = 1,
log2_channels = 0)
with open(fn, "wb") as output:
output.write( hdr )
output.write( quantized_packed )
def blah(*args):
# generate cosine/sine to mix with
twopift = 2 * numpy.pi * lo * numpy.array(numpy.arange(len(samples))/sr_in, dtype=numpy.double)
mixed_i = samples * numpy.cos( twopift )#( 2 * math.pi * lo * numpy.arange(len(samples))*1.0/sr_in )
mixed_q = samples * numpy.sin( twopift )#( 2 * math.pi * lo * numpy.arange(len(samples))*1.0/sr_in )
# we need a lowpass filter - take sr_out / 2 as Nyquist limited band
n_tap = 128
h = SIGNAL.firwin(n_tap, sr_out/1.0/sr_in)
# how many seconds?
ft_series_i = numpy.fft.fft(mixed_i)
ft_series_q = numpy.fft.fft(mixed_q)
# upsample
up_i = numpy.zeros(len(samples)*L)
up_i[::L] = mixed_i
up_q = numpy.zeros(len(samples)*L)
up_q[::L] = mixed_q
# 'kay. Now filter those upsampled series
series_f_i = SIGNAL.lfilter(h, 1, up_i)
series_f_q = SIGNAL.lfilter(h, 1, up_q)
# and downsample
down_i = series_f_i[::M]
down_q = series_f_q[::M]
# # Analysis + plot part
# # figure out which bits of the FFT to use.
# # if we have more samples than the actual sample rate of the signal no point in showing > n/2 points
# dt = float(len(series))/sr_in
# n_series = min(sr_in//2, len(series)//2)
# n_series_f = min(len(series_f)//2, (sr_in * L)//2)
# n_down = min(sr_out//2, int(len(up)/M/2))
# all_peaks = list(map(peaks_peak(0.8), [ft_series[:n_series], ft_series_f[:n_series_f], ft_down[:n_down]]))
# print("peaks in FFT of mixed signal:", all_peaks[0])
# print("peaks in FFT of upsampled+filtered mixed signal:",all_peaks[1]," +LO=",all_peaks[1]/dt+lo)
# print("peaks in FFT of downsampled signal:",all_peaks[2]," +LO=",all_peaks[2]/dt+lo)
#
# f, plots = plt.subplots(nrows=6, ncols=1)
# plots[0].plot( series )
# plots[1].plot( numpy.abs(ft_series[:n_series]) )
# # series_f is the upsampled-by-L-and-filtered signal
# plots[2].plot( series_f )
# plots[3].plot( numpy.abs(ft_series_f[:n_series_f]) )
# # downsampled signal has sr_out sample rate so after fft only 1/2 of the points are useful
# plots[4].plot( down )
# plots[5].plot( numpy.abs(ft_down[:n_down]) )
# #plt.show()
def ddc6(lo, sr_in, sr_out, samples):
import scipy.signal as SIGNAL
import matplotlib.pyplot as plt
# Get L, M
mult = lcm(sr_in, sr_out)
L = mult // sr_in
M = mult // sr_out
L = M = 2
print("DDC/sr_in={0} sr_out={1}, using sr_in * {2} / {3} = sr_out".format(sr_in, sr_out, L, M))
# generate cosine to mix with
mixed = samples * numpy.cos( math.pi * 2 * lo * numpy.arange(len(samples))*1.0/sr_in )
# we need a lowpass filter - take sr_out / 2 as Nyquist limited band
n_tap = 128
#h = SIGNAL.firwin(n_tap, sr_out/1.0/sr_in)
h = SIGNAL.firwin(n_tap, 0.1)
# how many seconds?
series = mixed #list(map(mixed, numpy.arange(0, dt, 1.0/sr_in)))
fft_len = 2048
nfft = len(series)//fft_len
ft_series = numpy.fft.fft(series[:nfft*fft_len].reshape((nfft, fft_len)))
# upsample
up = numpy.zeros(len(series)*L)
up[::L] = series
# 'kay. Now filter that upsampled series
series_f = SIGNAL.lfilter(h, 1, up)
ft_series_f = numpy.fft.fft(series_f[:nfft*fft_len*L].reshape((nfft, fft_len*L)))
# and downsample
down = series_f[::M]
fft_len2 = max(fft_len//M, 1024)
n_fft2 = max(len(series)//fft_len2, 1)
n_pts = min(n_fft2*fft_len2, len(series))
#ft_down = numpy.fft.fft(down[:n_pts].reshape((n_fft2, min(fft_len2, len(series)))), axis=0)
ft_down = numpy.fft.fft(down)
print("ft_series.shape=",ft_series.shape)
print("ft_series_f.shape=",ft_series_f.shape)
print("ft_down.shape=",ft_down.shape)
# Analysis + plot part
# figure out which bits of the FFT to use.
# if we have more samples than the actual sample rate of the signal no point in showing > n/2 points
dt = float(len(series))/sr_in
n_series = min(sr_in//2, len(series)//2)
n_series_f = min(len(series_f)//2, (sr_in * L)//2)
n_down = min(sr_out//2, int(len(up)/M/2))
all_peaks = list(map(peaks_peak(0.8), [ft_series[:n_series], ft_series_f[:n_series_f], ft_down[:n_down]]))
print("peaks in FFT of mixed signal:", all_peaks[0])
print("peaks in FFT of upsampled+filtered mixed signal:",all_peaks[1]," +LO=",all_peaks[1]/dt+lo)
print("peaks in FFT of downsampled signal:",all_peaks[2]," +LO=",all_peaks[2]/dt+lo)
f, plots = plt.subplots(nrows=6, ncols=1)
plots[0].plot( series )
#plots[1].plot( numpy.abs(ft_series[:n_series]) )
for i in range(ft_series.shape[0]):
plots[1].plot(ft_series[i])
# series_f is the upsampled-by-L-and-filtered signal
plots[2].plot( series_f )
#plots[3].plot( numpy.abs(ft_series_f[:n_series_f]) )
for i in range(ft_series_f.shape[0]):
plots[3].plot(ft_series_f[i])
# downsampled signal has sr_out sample rate so after fft only 1/2 of the points are useful
plots[4].plot( down )
plots[5].plot( numpy.abs(ft_down[:n_down]) )
def tst1():
import matplotlib.pyplot as plt
sr = 4000#2048
z = list(map(sines_embedded_in_noise([768.0, 128.0, 110.0], SNR=0.2), numpy.linspace(0, 1, sr)))
#z = list(map(sines_embedded_in_noise([63.4], SNR=1), numpy.linspace(0, 1, 2048)))
ftz = numpy.fft.fft(z)
f, plots = plt.subplots(nrows=2, ncols=1)
plots[0].plot(z); plots[1].plot( numpy.abs(ftz[0:sr//2]) ); plt.show()
########################################################################################################
##
##
## Now we want to generate a time series from spectra; synthesizerts!
##
##
########################################################################################################
def gaussian_noise_s(npoint, amplitude=1.0, mean=0.0):
def gen(ts):
return amplitude * numpy.random.randn(npoint) + mean
return gen
def tones_s(npoint, tones):
try:
for toneidx in tones:
assert toneidx>=0 and toneidx<npoint
except TypeError:
assert tones>=0 and tones<npoint
template = numpy.zeros(npoint)
template[tones] = 1.0
def gen(ts):
return template