forked from rtmrtmrtmrtm/weakmon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weakutil.py
1373 lines (1126 loc) · 38.3 KB
/
weakutil.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
#
# shared support routines for weak*.py
#
#
# read weak.ini
# e.g. weakcfg.get("wsprmon", "mycall") -> None or "W1XXX"
#
try:
import configparser
except:
from six.moves import configparser
import threading
import numpy
import scipy
import scipy.signal
import scipy.fftpack
import wave
import time
import sys
import os
import math
import random
import types
#import scikits.samplerate
have_fftw = False
try:
import pyfftw.interfaces.numpy_fft
have_fftw = True
except:
pass
def cfg(program, key):
cfg = configparser.SafeConfigParser()
cfg.read(['weak-local.cfg', 'weak.cfg'])
if cfg.has_option(program, key):
return cfg.get(program, key)
return None
# make a butterworth IIR bandpass filter
def butter_bandpass(lowcut, highcut, samplerate, order=5):
# http://wiki.scipy.org/Cookbook/ButterworthBandpass
nyq = 0.5 * samplerate
low = lowcut / nyq
high = highcut / nyq
b, a = scipy.signal.butter(order, [low, high], btype='bandpass')
return b, a
def butter_highpass(cut, samplerate, order=5):
nyq = 0.5 * samplerate
cut = cut / nyq
b, a = scipy.signal.butter(order, cut, btype='highpass')
return b, a
def butter_lowpass(cut, samplerate, order=5):
nyq = 0.5 * samplerate
cut = cut / nyq
b, a = scipy.signal.butter(order, cut, btype='lowpass')
return b, a
def cheby_lowpass(cut, rate):
passhz = cut - 0.0
stophz = cut + 50.0
b, a = scipy.signal.iirdesign(2.0*passhz/rate,
2.0*stophz/rate,
0.5, # ripple_pass
60.0, # atten_stop
ftype = "cheby2",
output="ba")
return b, a
def new_cheby_lowpass(passhz, stophz, pass_ripple, stop_atten, rate):
b, a = scipy.signal.iirdesign(2.0*passhz/rate,
2.0*stophz/rate,
pass_ripple, # ripple_pass
stop_atten, # atten_stop
ftype = "cheby1",
output="ba")
return b, a
def old_cheby_highpass(cut, rate):
passhz = cut + 0.0
stophz = cut - 75.0
b, a = scipy.signal.iirdesign(2.0*passhz/rate,
2.0*stophz/rate,
0.5, # ripple_pass
60.0, # atten_stop
ftype = "cheby1",
output="ba")
return b, a
def cheby_highpass(cut, rate):
cut += 25
passhz = cut + 25.0
stophz = cut - 75.0
b, a = scipy.signal.iirdesign(2.0*passhz/rate,
2.0*stophz/rate,
0.05, # ripple_pass
50.0, # atten_stop
ftype = "cheby2",
output="ba")
return b, a
# passhz - stophz must be pretty big, maybe 100 hz?
def new_cheby_highpass(stophz, passhz, rate, ripple_pass=0.5, atten_stop=50.0):
stophz = max(0.0, stophz)
passhz = max(stophz + 1.0, passhz)
b, a = scipy.signal.iirdesign(2.0*passhz/rate,
2.0*stophz/rate,
ripple_pass,
atten_stop,
ftype = "cheby2",
output="ba")
return b, a
# FIR bandpass filter
# http://stackoverflow.com/questions/16301569/bandpass-filter-in-python
def bandpass_firwin(ntaps, lowcut, highcut, fs, window='hamming'):
nyq = 0.5 * fs
taps = scipy.signal.firwin(ntaps, [lowcut, highcut], nyq=nyq, pass_zero=False,
window=window, scale=False)
return taps
#
# frequency shift via hilbert transform (like SSB).
# Lev Givon, https://gist.github.com/lebedov/4428122
#
def nextpow2(x):
"""Return the first integer N such that 2**N >= abs(x)"""
return int(numpy.ceil(numpy.log2(numpy.abs(x))))
def pre_freq_shift(x):
N_orig = len(x)
N_padded = 2**nextpow2(N_orig)
x0 = numpy.append(x, numpy.zeros(N_padded-N_orig, x.dtype))
hilb = scipy.signal.hilbert(x0)
return hilb
# f_shift in Hz.
# dt is 1 / sample rate (e.g. 1.0/11025).
# frequencies near the low and high ends of the spectrum are
# reflected, so large shifts will be a problem.
# if pre_hilbert!=None, it should be the result of pre_freq_shift(x).
def freq_shift(x, f_shift, dt, pre_hilbert=None):
"""Shift the specified signal by the specified frequency."""
# Pad the signal with zeros to prevent the FFT invoked by the transform from
# slowing down the computation:
N_orig = len(x)
N_padded = 2**nextpow2(N_orig)
t = numpy.arange(0, N_padded)
lo = numpy.exp(2j*numpy.pi*f_shift*dt*t)
if isinstance(pre_hilbert, type(None)) == False:
hilb = pre_hilbert
assert len(hilb) == len(x) + N_padded - N_orig
else:
hilb = pre_freq_shift(x)
h = hilb*lo
ret = h[:N_orig].real
return ret
def one_test_freq_shift(rate, hz, n, f_shift):
t1 = costone(rate, hz, n)
t2 = freq_shift(t1, f_shift, 1.0 / rate)
expected = costone(rate, hz + f_shift, n)
if False:
import matplotlib.pyplot as plt
i = 1000
j = i + 1000
plt.plot(t1[i:j])
plt.plot(t2[i:j])
plt.plot(expected[i:j])
plt.show()
diff = t2 - expected
x = math.sqrt(numpy.mean(diff * diff))
return x
def test_freq_shift():
x = one_test_freq_shift(12000, 511, 12000*13, 6.25 / 4)
print(x)
t1 = numpy.random.rand(12000 * 13)
t1 += costone(12000, 5970, 12000 * 13)
t1 += costone(12000, 511, 12000 * 13)
#t1 += costone(12000, 10, 12000 * 13)
t2 = freq_shift(t1, 60, 1.0 / 12000)
t3 = freq_shift(t2, -60, 1.0 / 12000)
diff = t3 - t1
x = math.sqrt(numpy.mean(diff * diff))
print(x)
writewav(t1, "t1.wav", 12000)
writewav(t2, "t2.wav", 12000)
writewav(t3, "t3.wav", 12000)
# caller supplies two shifts, in hza[0] and hza[1].
# shift x[0] by hza[0], ..., x[-1] by hza[1]
# corrects for phase change:
# http://stackoverflow.com/questions/3089832/sine-wave-glissando-from-one-pitch-to-another-in-numpy
# sadly, more expensive than piece-wise freq_shift() because
# the ramp part does noticeable work.
def freq_shift_ramp(x, hza, dt):
N_orig = len(x)
N_padded = 2**nextpow2(N_orig)
t = numpy.arange(0, N_padded)
f_shift = numpy.linspace(hza[0], hza[1], len(x))
f_shift = numpy.append(f_shift, hza[1]*numpy.ones(N_padded-len(x)))
pc1 = f_shift[:-1] - f_shift[1:]
phase_correction = numpy.add.accumulate(
t * dt * numpy.append(numpy.zeros(1), 2*numpy.pi*pc1))
lo = numpy.exp(1j*(2*numpy.pi*dt*f_shift*t + phase_correction))
x0 = numpy.append(x, numpy.zeros(N_padded-N_orig, x.dtype))
h = scipy.signal.hilbert(x0)*lo
ret = h[:N_orig].real
return ret
# avoid most of the round-up-to-power-of-two penalty by
# doing log-n shifts. discontinuity at boundaries,
# but that's OK for JT65 2048-sample symbols.
def freq_shift_hack(x, hza, dt):
a = freq_shift_hack_iter(x, hza, dt)
return numpy.concatenate(a)
def freq_shift_hack_iter(x, hza, dt):
if len(x) <= 4096:
return [ freq_shift(x, (hza[0] + hza[1]) / 2.0, dt) ]
lg = nextpow2(len(x))
if len(x) == 2**lg:
return [ freq_shift_ramp(x, hza, dt) ]
i1 = 2**(lg-1)
hz_i1 = hza[0] + (i1 / float(len(x)))*(hza[1] - hza[0])
ret = [ freq_shift_ramp(x[0:i1], [ hza[0], hz_i1 ], dt) ]
ret += freq_shift_hack_iter(x[i1:], [ hz_i1, hza[1] ], dt)
return ret
# pure sin tone, n samples long.
def sintone(rate, hz, n):
x = numpy.linspace(0, 2 * hz * (n / float(rate)) * numpy.pi, n,
endpoint=False)
tone = numpy.sin(x)
return tone
# pure cos tone, n samples long.
def costone(rate, hz, n, phase0=0.0):
x = numpy.linspace(phase0, phase0 + 2 * hz * (n / float(rate)) * numpy.pi, n,
endpoint=False)
tone = numpy.cos(x)
return tone
# parameter
fos_threshold = 0.5
# cache
fos_mu = threading.Lock()
fos_hz = None
fos_fft = None
fos_n = None
# shift signal a up by hz, returning
# the rfft of the resulting signal.
# do it by convolving ffts.
# use numpy.fft.irfft(ret, len(a)) to get the signal.
# intended for single power-of-two-length JT65 symbols.
# faster than freq_shift() if you need the fft (not the
# actual resulting signal).
def fft_of_shift(a, hz, rate):
global fos_hz, fos_n, fos_fft
afft = rfft(a)
bin_hz = rate / float(len(a))
# we want this many fft bins on either side of the tone.
pad_bins = 10
# shift the tone up by integral bins until it is
# enough above zero that we get fft bins on either side.
# XXX if hz is more than 10 bins above zero, negative shift.
if hz >= 0:
shift_bins = max(0, pad_bins - int(hz / bin_hz))
else:
shift_bins = int((-hz) / bin_hz) + pad_bins
hz += shift_bins * bin_hz
# fos_mu.acquire()
if fos_n == len(a) and abs(fos_hz - hz) < fos_threshold:
lofft = fos_fft
else:
lo = sintone(rate, hz, len(a))
lofft = rfft(lo)
fos_hz = hz
fos_fft = lofft
fos_n = len(a)
# fos_mu.release()
lo_bin = int(hz / bin_hz)
lofft = lofft[0:lo_bin+pad_bins]
outfft = numpy.convolve(lofft, afft, 'full')
outfft = outfft[shift_bins:]
outfft = outfft[0:len(afft)]
return outfft
# https://gist.github.com/endolith/255291
# thank you, endolith.
def parabolic(f, x):
"""Quadratic interpolation for estimating the true position of an
inter-sample maximum when nearby samples are known.
f is a vector and x is an index for that vector.
Returns (vx, vy), the coordinates of the vertex of a parabola that goes
through point x and its two neighbors.
Example:
Defining a vector f with a local maximum at index 3 (= 6), find local
maximum if points 2, 3, and 4 actually defined a parabola.
In [3]: f = [2, 3, 1, 6, 4, 2, 3, 1]
In [4]: parabolic(f, argmax(f))
Out[4]: (3.2142857142857144, 6.1607142857142856)
"""
if x < 1 or x+1 >= len(f):
return None
denom = (f[x-1] - 2 * f[x] + f[x+1])
if denom == 0.0:
return None
xv = 1/2. * (f[x-1] - f[x+1]) / denom + x
yv = f[x] - 1/4. * (f[x-1] - f[x+1]) * (xv - x)
return (xv, yv)
# indexed by window length
fff_cached_windows = { }
def init_freq_from_fft(n):
n = int(n)
if not n in fff_cached_windows:
fff_cached_windows[n] = scipy.signal.blackmanharris(n)
# https://gist.github.com/endolith/255291
def freq_from_fft(sig, rate, minf, maxf):
global fff_cached_windows
n = len(sig)
assert n in fff_cached_windows
fa = sig
fa = fa * fff_cached_windows[n]
#fa = abs(numpy.fft.rfft(fa))
fa = arfft(fa)
# find max between minf and maxf
mini = int(minf * n / rate)
maxi = int(maxf * n / rate)
i = numpy.argmax(fa[mini:maxi]) + mini # peak bin
if i < 1 or i+1 >= len(fa) or fa[i] <= 0.0:
return None
if fa[i-1] == 0.0 or fa[i] == 0.0 or fa[i+1] == 0.0:
return None
xp = parabolic(numpy.log(fa[i-1:i+2]), 1) # interpolate
if xp == None:
return None
true_i = xp[0] + i - 1
return rate * true_i / float(n) # convert to frequency
# like freq_from_fft, but in units of FFT bins.
def bin_from_fft(sig, rate, bin):
global fff_cached_windows
n = len(sig)
assert n in fff_cached_windows
sig = sig * fff_cached_windows[n]
fa = arfft(sig)
xp = parabolic(numpy.log(fa[bin-1:bin+2]), 1) # interpolate
if xp == None:
return None
true_bin = xp[0] + bin - 1
return true_bin
def moving_average(a, n):
ret = numpy.cumsum(a, dtype=float)
ret[n:] = ret[n:] - ret[:-n]
ret = ret[n - 1:] / n
if (n % 2) == 1:
slop = n // 2 # ret is short on either end by this
assert len(ret) == len(a) - 2*slop
ret = numpy.concatenate(( ret[0:slop], ret, ret[-slop:] ))
else:
slop = n // 2 # ret is short on either end by this
assert len(ret) == len(a) - 2*slop + 1
ret = numpy.append(ret[0:slop], ret)
if slop > 1:
ret = numpy.append(ret, ret[-(slop-1):])
return ret
# IQ -> USB
# the result is bad at the start and end,
# so not ideal for processing a sequence of
# sample blocks.
def iq2usb_internal(iq):
ii = iq.real
qq = iq.imag
nn = len(iq)
ii = numpy.real(scipy.signal.hilbert(ii, nn)) # delay to match hilbert(Q)
qq = numpy.imag(scipy.signal.hilbert(qq, nn))
ssb = numpy.subtract(ii, qq) # usb from phasing method
#ssb = numpy.add(ii, qq) # lsb from phasing method
assert len(iq) == len(ssb)
return ssb
# do it in overlapping smallish chunks, so the FFTs are smaller
# and faster.
# always returns the same number of samples as you give it.
def iq2usb(iq):
guard = 256 # overlap between successive chunks
chunksize = 8192 - 2*guard
bufbuf = [ ]
oi = 0 # output index, == sum of bufbuf[] lengths
while oi < len(iq):
sz = min(chunksize, len(iq) - oi)
buf = iq[oi:oi+sz]
# pad so always chunksize
if len(buf) < chunksize:
buf = numpy.append(buf, numpy.zeros(chunksize - len(buf)))
# prepend the guard
if oi >= guard:
buf = numpy.append(iq[oi-guard:oi], buf)
else:
buf = numpy.append(numpy.zeros(guard), buf)
# append the guard
n1 = min(guard, len(iq) - (oi + chunksize))
n1 = max(n1, 0)
if n1 > 0:
buf = numpy.append(buf, iq[oi+chunksize:oi+chunksize+n1])
if n1 < guard:
buf = numpy.append(buf, numpy.zeros(guard - n1))
assert len(buf) == chunksize + 2*guard
z = iq2usb_internal(buf)
assert len(z) == chunksize + 2*guard
bufbuf.append(z[guard:-guard])
oi += len(bufbuf[-1])
buf = numpy.concatenate(bufbuf)
assert len(buf) >= len(iq)
buf = buf[0:len(iq)]
return buf
# simple measure of distortion introduced by iq2usb.
def one_test_iq2usb(rate, hz, n):
ii = costone(rate, hz, n)
qq = sintone(rate, hz, n)
iq = ii + 1j*qq
usb = iq2usb(iq)
# usb ought to be 2*ii
if False:
import matplotlib.pyplot as plt
plt.plot(ii[0:100])
plt.plot(qq[0:100])
plt.plot(usb[0:100])
plt.show()
diff = (usb / 2.0) - ii
x = math.sqrt(numpy.mean(diff * diff))
return x
# original (no chunking):
# 32000 511 1333: 0.038 0.001
# 32000 511 27777: 0.010 0.016
# 32000 511 320001: 0.000 6.345
# chunksize 8192, guard 128
# 32000 511 1333: 0.023 0.002
# 32000 511 27777: 0.006 0.006
# 32000 511 320001: 0.002 0.041
def test_iq2usb():
for [ rate, hz, n ] in [
[ 32000, 511, 1333 ],
[ 32000, 511, 27777 ],
[ 32000, 511, 320001 ]
]:
t0 = time.time()
x = one_test_iq2usb(rate, hz, n)
t1 = time.time()
print("%d %d %d: %.3f %.3f" % (rate, hz, n, x, t1 - t0))
# "scipy" -- scipy.signal.resample(), the default
# "interp" -- numpy.interp()
# "rabbit" -- Secret Rabbit Code
which_resampler = "scipy"
# change sampling rate from from_rate to to_rate.
# buf must already be low-pass filtered if to_rate < from_rate.
# note that result probably has slightly different
# length in seconds, so caller may want to adjust.
def resample(buf, from_rate, to_rate):
# how many samples do we want?
target = int(round((len(buf) / float(from_rate)) * to_rate))
if which_resampler == "rabbit":
ratio = to_rate / float(from_rate)
buf1 = scikits.samplerate.resample(buf, ratio, 'sinc_best')
return buf1
if from_rate == to_rate:
return buf
if from_rate == to_rate * 2:
buf = buf[0::2]
return buf
if from_rate == to_rate * 4:
buf = buf[0::4]
return buf
if False:
if from_rate == to_rate * 10:
print("resample/10", from_rate, to_rate)
buf = buf[0::10]
return buf
if from_rate == to_rate * 20:
print("resample/20", from_rate, to_rate)
buf = buf[0::20]
return buf
if from_rate == to_rate * 30:
print("resample/30", from_rate, to_rate)
buf = buf[0::30]
return buf
if from_rate == to_rate * 40:
print("resample/40", from_rate, to_rate)
buf = buf[0::40]
return buf
# 11025 -> 441, for wwvmon.py.
if from_rate == to_rate * 25:
buf = buf[0::25]
return buf
# 11025 -> 315, for wwvmon.py.
if from_rate == to_rate * 35:
buf = buf[0::35]
return buf
if from_rate == to_rate * 64:
buf = buf[0::64]
return buf
if which_resampler == "scipy":
# seems to produce better results than numpy.interp() but
# is slower, sometimes much slower.
# pad to power of two length
nn = 2**nextpow2(len(buf))
buf = numpy.append(buf, numpy.zeros(nn - len(buf)))
want = (len(buf) / float(from_rate)) * to_rate
want = int(round(want))
buf = scipy.signal.resample(buf, want)
elif which_resampler == "interp":
secs = len(buf)*(1.0/from_rate)
ox = numpy.arange(0, secs, 1.0 / from_rate)
ox = ox[0:len(buf)]
nx = numpy.arange(0, secs, 1.0 / to_rate)
buf = numpy.interp(nx, ox, buf)
else:
assert False
if len(buf) > target:
buf = buf[0:target]
return buf
# gadget to low-pass-filter and re-sample a multi-block
# stream without losing fractional samples at block
# boundaries, which would hurt phase-shift demodulators
# like WWVB.
class Resampler:
def __init__(self, from_rate, to_rate, order=7):
self.from_rate = from_rate
self.to_rate = to_rate
if self.from_rate > self.to_rate:
# prepare a filter to precede resampling.
self.filter = butter_lowpass(0.45 * self.to_rate,
from_rate,
order)
self.zi = scipy.signal.lfiltic(self.filter[0],
self.filter[1],
[0])
# total number of input and output samples,
# so we can insert/delete to keep long-term
# rates correct.
self.nin = 0
self.nout = 0
if which_resampler == "rabbit":
self.rabbit = scikits.samplerate.Resampler('sinc_best', channels=1)
# how much will output be delayed?
# in units of output samples.
def delay(self, hz):
if self.from_rate > self.to_rate:
# convert hz to radians per sample,
# at input sample rate.
rps = (1.0 / hz) * (2 * math.pi) / self.from_rate
gd = scipy.signal.group_delay(self.filter, w=[rps])
n = (gd[1][0] / self.from_rate) * self.to_rate
return n
else:
return 0
def resample(self, buf):
# if resample() uses FFT, then handing it huge chunks is
# slow. so cut big buffers into one-second chunks.
a = [ ]
i = 0
if self.from_rate > 20000:
big = self.from_rate // 2
else:
big = self.from_rate
while i < len(buf):
left = len(buf) - i
chunk = None
if left > 1.5*big:
chunk = big
else:
chunk = left
a.append(self.resample1(buf[i:i+chunk]))
i += chunk
b = numpy.concatenate(a)
return b
def resample1(self, buf):
inlen = len(buf)
savelast = buf[-20:]
insec = self.nin / float(self.from_rate)
outsec = self.nout / float(self.to_rate)
if insec - outsec > 0.5 / self.from_rate:
ns = (insec - outsec) / (1.0 / self.from_rate)
ns = int(round(ns))
if ns < 1:
ns = 1
assert len(self.last) >= ns
buf = numpy.append(self.last[-ns:], buf)
if outsec - insec > 0.5 / self.from_rate:
ns = (outsec - insec) / (1.0 / self.from_rate)
ns = int(round(ns))
if ns < 1:
ns = 1
buf = buf[ns:]
self.last = savelast
if self.from_rate > self.to_rate:
# low-pass filter.
zi = scipy.signal.lfilter(self.filter[0],
self.filter[1],
buf,
zi=self.zi)
buf = zi[0]
self.zi = zi[1]
if which_resampler == "rabbit":
ratio = self.to_rate / float(self.from_rate)
buf = self.rabbit.process(buf, ratio, end_of_input=False)
else:
buf = resample(buf, self.from_rate, self.to_rate)
self.nin += inlen
self.nout += len(buf)
return buf
def one_test_resampler(from_rate, to_rate):
hz = 100
t1 = costone(from_rate, hz, from_rate*10)
r = Resampler(from_rate, to_rate)
i = 0
bufbuf = [ ]
while i < len(t1):
n = random.randint(1, from_rate)
buf = r.resample(t1[i:i+n])
bufbuf.append(buf)
i += n
t2 = numpy.concatenate(bufbuf)
expecting = costone(to_rate, hz, to_rate*10)
delay = r.delay(hz)
delay = int(round(delay))
expecting = numpy.append(numpy.zeros(delay), expecting)
expecting = expecting[0:to_rate*10]
diff = t2 - expecting
x = math.sqrt(numpy.mean(diff * diff))
if False:
import matplotlib.pyplot as plt
i0 = len(diff) - 200
i1 = i0 + 200
plt.plot(expecting[i0:i1])
plt.plot(t2[i0:i1])
plt.plot(diff[i0:i1])
plt.show()
return x
# measure Resampler distortion.
# not very revealing since the filter delay
# prevents easy comparison.
def test_resampler():
global resample_interp
ori = resample_interp
for [ r1, r2 ] in [
[ 32000, 12000 ],
[ 3000, 400 ],
[ 3000, 300 ],
[ 8000, 400 ],
[ 8000, 300 ],
]:
resample_interp = False
x = one_test_resampler(r1, r2)
print("%d %d False: %.3f" % (r1, r2, x))
#resample_interp = True
#x = one_test_resampler(r1, r2)
#print("%d %d True: %.3f" % (r1, r2, x))
resample_interp = ori
which_fft = "scipy" # numpy, scipy, fftw
if have_fftw == False and which_fft == "fftw":
which_fft = "scipy"
fftw_info = { }
fftw_n_info_79 = { }
fftw_n_info_103 = { }
fftw_i_info = { }
fft_inited = False
fftw_inited = False
fft_size_list = [ ]
# specify sizes which fftw should optimize for
def fft_sizes(sizes):
global fft_size_list
for sz in sizes:
if not (sz in fft_size_list):
assert fft_inited == False and fftw_inited == False
fft_size_list += [ sz ]
def init_fft():
global fft_inited, fftw_inited
##if pyfftw.interfaces.cache.is_enabled() == False:
## pyfftw.interfaces.cache.enable()
## pyfftw.interfaces.cache.set_keepalive_time(30.0)
if have_fftw and which_fft == "fftw" and fftw_inited == False:
fftw_inited = True
print("starting fftw init ", fft_size_list)
sys.stdout.flush()
for size in fft_size_list:
if not (size in fftw_info):
a = pyfftw.empty_aligned(size, dtype='float64')
fft = pyfftw.builders.rfft(a, planner_effort='FFTW_PATIENT')
fftw_info[size] = fft
# for FT8's 79 symbols
a = pyfftw.empty_aligned((79,size), dtype='float64')
fft = pyfftw.builders.rfftn(a, axes=[1],
planner_effort='FFTW_PATIENT')
fftw_n_info_79[size] = fft
# for FT4's 103 symbols
a = pyfftw.empty_aligned((103,size), dtype='float64')
fft = pyfftw.builders.rfftn(a, axes=[1],
planner_effort='FFTW_PATIENT')
fftw_n_info_103[size] = fft
a = pyfftw.empty_aligned((size//2)+1, dtype='complex128')
fft = pyfftw.builders.irfft(a, planner_effort='FFTW_PATIENT')
fftw_i_info[(size//2)+1] = fft
print("done with fftw init")
sys.stdout.flush()
if fft_inited == False:
fft_inited = True
for size in fft_size_list:
# warm up twiddle cache
a = numpy.random.random(size)
rfft(a)
# wrapper for either numpy or scipy rfft(),
# followed by abs().
# scipy rfft() is nice b/c it supports float32.
# but not faster for 2048-point FFTs.
def arfft(x):
init_fft()
if which_fft == "numpy" or which_fft == "fftw":
y = rfft(x)
y = abs(y)
return y
if which_fft == "scipy":
assert (len(x) % 2) == 0
y = scipy.fftpack.rfft(x)
if type(y[0]) == numpy.float32:
cty = numpy.complex64
elif type(y[0]) == numpy.float64:
cty = numpy.complex128
else:
assert False
# y = [ Re0, Re1, Im1, Re2, Im2, ..., ReN ]
#y1 = numpy.sqrt(numpy.add(numpy.square(y[1:-1:2]),
# numpy.square(y[2:-1:2])))
y0 = abs(y[0])
yn = abs(y[-1])
y = abs(y[1:-1].view(cty))
y = numpy.concatenate(([y0], y, [yn]))
return y
assert False
# wrapper for either numpy or scipy rfft().
# scipy rfft() is nice b/c it supports float32.
# but not faster for 2048-point FFTs.
def rfft(x):
global fftw_info
init_fft()
if which_fft == "numpy":
y = numpy.fft.rfft(x)
return y
if which_fft == "fftw":
y = fftw_info[len(x)](x)
y = numpy.copy(y)
return y
if which_fft == "scipy":
assert (len(x) % 2) == 0
y = scipy.fftpack.rfft(x)
if type(y[0]) == numpy.float32:
cty = numpy.complex64
elif type(y[0]) == numpy.float64:
cty = numpy.complex128
else:
assert False
# y = [ Re0, Re1, Im1, Re2, Im2, ..., ReN ]
y1 = y[1:-1].view(cty)
y = numpy.concatenate((y[0:1], y1, y[-1:]))
return y
assert False
# x should be a numpy array of complex numbers,
# as generated by rfft().
def irfft(x):
global fftw_info
init_fft()
if which_fft == "numpy":
y = numpy.fft.irfft(x)
return y
if which_fft == "fftw":
y = fftw_i_info[len(x)](x)
y = numpy.copy(y)
return y
if which_fft == "scipy":
if type(x[0]) == numpy.complex64:
ty = numpy.float32
else:
ty = numpy.float64
# xx wants to be [ Re0, Re1, Im1, Re2, Im2, ..., ReN ]
xx = numpy.zeros(2 * len(x) - 2, dtype=ty)
xx[1::2] = numpy.real(x[1:])
xx[2::2] = numpy.imag(x[1:-1])
xx[0] = numpy.real(x[0])
y = scipy.fftpack.irfft(xx)
return y
assert False
def time_irfft():
# laptop. similar on Linux and FreeBSD, though numpy is only 2x slower than scipy.
# numpy 0.029132
# scipy 0.009165
# fftw 0.003470
n = 480
fft_sizes([n])
global which_fft
v1 = numpy.random.random(n)
v2 = rfft(v1)
which_fft = "numpy"
init_fft()
t0 = time.time()
for i in range(0, 1000):
v3 = irfft(v2)
t1 = time.time()
assert len(v3) == n
print("%s %.6f" % (which_fft, t1 - t0))
which_fft = "scipy"
init_fft()
t0 = time.time()
for i in range(0, 1000):
v3 = irfft(v2)
t1 = time.time()
assert len(v3) == n
print("%s %.6f" % (which_fft, t1 - t0))
which_fft = "fftw"
init_fft()
t0 = time.time()
for i in range(0, 1000):
v3 = irfft(v2)
t1 = time.time()
assert len(v3) == n
print("%s %.6f" % (which_fft, t1 - t0))
# you may need to numpy.copy() the input array.
def rfftn(x, axes=None):
global fftw_n_info_79, fftw_n_info_103
init_fft()
if which_fft == "numpy" or which_fft == "scipy":
y = numpy.fft.rfftn(x, axes=axes)
return y
if which_fft == "fftw":
assert axes == [ 1 ]
if x.shape[0] == 79:
y = fftw_n_info_79[x.shape[1]](x)
elif x.shape[0] == 103:
y = fftw_n_info_103[x.shape[1]](x)
else:
assert False
return y
assert False