|
| 1 | +#! /usr/bin/env python |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from matplotlib import pyplot as plt |
| 5 | +from scipy.stats import itemfreq |
| 6 | + |
| 7 | +import numpy as np |
| 8 | +import scipy.signal as sps |
| 9 | + |
| 10 | +CD_BASE_FREQUENCY = 4321800.0 # Hz |
| 11 | +SAMPLE_FREQUENCY = 28.636e6 # Hz |
| 12 | + |
| 13 | +FREQ_MHZ = (315.0 / 88.0) * 8.0 |
| 14 | +FREQ_HZ = FREQ_MHZ * 1000000.0 |
| 15 | +NYQUIST_MHZ = FREQ_MHZ / 2 |
| 16 | + |
| 17 | +data = np.fromfile("chopin8.cdraw", dtype = np.uint8) |
| 18 | + |
| 19 | +# remove the first samples because they are strange (lower amplitude) |
| 20 | +data = data[2650:] |
| 21 | + |
| 22 | +# convert to single-precision floats |
| 23 | +#data = data.astype(np.float32) |
| 24 | + |
| 25 | +# fewer errors if we filter as double precision |
| 26 | +data = data.astype(np.float64) |
| 27 | + |
| 28 | +# subtract DC component |
| 29 | +dc = data.mean() |
| 30 | +data -= dc |
| 31 | + |
| 32 | +# without filter: 340 errors |
| 33 | + |
| 34 | +# 91 errors |
| 35 | +bandpass = sps.firwin(97, [.08/NYQUIST_MHZ, 1.20/NYQUIST_MHZ], pass_zero=False) |
| 36 | +# 88 errors |
| 37 | +bandpass = sps.firwin(97, [.075/NYQUIST_MHZ, 1.20/NYQUIST_MHZ], pass_zero=False) |
| 38 | +# 66 errors, 53 if double precision |
| 39 | +bandpass = sps.firwin(97, [.075/NYQUIST_MHZ, 1.50/NYQUIST_MHZ], pass_zero=False) |
| 40 | +# 47 (double precision) |
| 41 | +bandpass = sps.firwin(97, [.100/NYQUIST_MHZ, 1.50/NYQUIST_MHZ], pass_zero=False) |
| 42 | +# 44 (double precision) |
| 43 | +bandpass = sps.firwin(91, [.100/NYQUIST_MHZ, 1.50/NYQUIST_MHZ], pass_zero=False) |
| 44 | +# 40 (double precision) |
| 45 | +bandpass = sps.firwin(91, [.095/NYQUIST_MHZ, 1.70/NYQUIST_MHZ], pass_zero=False) |
| 46 | +data = sps.lfilter(bandpass, 1.0, data) |
| 47 | + |
| 48 | +# filter to binary signal |
| 49 | +data = (data > 0.0) |
| 50 | + |
| 51 | +transition = np.diff(data) != 0 |
| 52 | +transition = np.insert(transition, 0, False) # The first sample is never a transition. |
| 53 | + |
| 54 | +print "data", data.shape, data.dtype |
| 55 | +print "transition", transition.shape, transition.dtype |
| 56 | + |
| 57 | +runLengths = np.diff(np.where(transition)[0]) |
| 58 | + |
| 59 | +# fetch run signal values. The last transition |
| 60 | +# isn't part of a well-defined run, so we don't need it. |
| 61 | + |
| 62 | +runValues = data[transition].astype(np.int8) |
| 63 | +runValues = runValues[:-1] |
| 64 | + |
| 65 | +print "runLengths", runLengths.shape, runLengths.dtype |
| 66 | +print "runValues", runValues.shape, runValues.dtype |
| 67 | + |
| 68 | +totalRunlength0 = np.sum(runLengths[runValues == 0]) |
| 69 | +totalRunlength1 = np.sum(runLengths[runValues == 1]) |
| 70 | + |
| 71 | +bias = (totalRunlength0 - totalRunlength1) / (SAMPLE_FREQUENCY * len(runLengths)) |
| 72 | +print "bias: {} seconds".format(bias) |
| 73 | + |
| 74 | +runDurations = runLengths / SAMPLE_FREQUENCY # to SECONDS |
| 75 | + |
| 76 | +runDurations[runValues == 0] -= bias |
| 77 | +runDurations[runValues == 1] += bias |
| 78 | + |
| 79 | +runDurations = runDurations * CD_BASE_FREQUENCY # to CD BASE FREQUENCY TICKS |
| 80 | + |
| 81 | +if False: |
| 82 | + |
| 83 | + print "plotting ..." |
| 84 | + |
| 85 | + freqAll = itemfreq(runDurations) |
| 86 | + freq0 = itemfreq(runDurations[runValues == 0]) |
| 87 | + freq1 = itemfreq(runDurations[runValues == 1]) |
| 88 | + |
| 89 | + plt.subplot(411) |
| 90 | + plt.title("All runs (bias corrected)") |
| 91 | + plt.xlim(0, 13) |
| 92 | + plt.plot(freqAll[:, 0], freqAll[:, 1], '.-') |
| 93 | + |
| 94 | + plt.subplot(412) |
| 95 | + plt.title("Bias-corrected zero runs ") |
| 96 | + plt.xlim(0, 13) |
| 97 | + plt.plot(freq0[:, 0], freq0[:, 1], '.-') |
| 98 | + |
| 99 | + plt.subplot(413) |
| 100 | + plt.title("Bias-corrected one runs") |
| 101 | + plt.xlim(0, 13) |
| 102 | + plt.plot(freq1[:, 0], freq1[:, 1], '.-') |
| 103 | + |
| 104 | + plt.subplot(414) |
| 105 | + plt.title("Bias-corrected zero/one runs, overlayed") |
| 106 | + plt.xlim(0, 13) |
| 107 | + plt.plot(freq0[:, 0], freq0[:, 1], '.-') |
| 108 | + plt.plot(freq1[:, 0], freq1[:, 1], '.-') |
| 109 | + |
| 110 | + plt.savefig("chopin8.pdf") |
| 111 | + plt.close() |
| 112 | + |
| 113 | +if True: |
| 114 | + |
| 115 | + print "writing file ..." |
| 116 | + |
| 117 | + with open("chopin8-bits.txt", "w") as f: |
| 118 | + for (value, duration) in zip(runValues, runDurations): |
| 119 | + duration = int(round(duration)) # to integer |
| 120 | + f.write(str(value) * duration) |
0 commit comments