Skip to content

Instantly share code, notes, and snippets.

@tonyarkles
Created February 19, 2014 05:13
Show Gist options
  • Save tonyarkles/9086408 to your computer and use it in GitHub Desktop.
Save tonyarkles/9086408 to your computer and use it in GitHub Desktop.
% Recorded the wav output from rtl_fm at 32k samples/sec when tuned to a TI CC2500 radio set to 2400 baud 2-FSK
w = wavread('cc2500-v1.wav');
w = w(500:2200);
bitwidth = 32000 / 2400; % (samples/sec) / (bits/sec) = (samples/bit)
header_bits = 32;
N = floor(header_bits * bitwidth);
ideal_header = zeros(1,N); % (string of 0xAA 0xAA 0xAA 0xAA, 1 is
% positive, 0 negative)
bit = 1; bitval = 1;
for i = 1:N
if i > floor(bit*bitwidth)
bit = bit + 1;
bitval = -bitval;
end
ideal_header(i) = bitval;
end
% find out where the header starts in the wave by finding the
% cross-correlation between the wave and the ideal header
[c_wi, lags] = xcorr(w, ideal_header);
[peak, i] = max(c_wi);
% truncate the wav so that it starts at the index of the peak in the
% x-correlation.
w = w(lags(i):length(w));
% figure out how many bits there are
nbits = floor(length(w)/bitwidth);
bits = zeros(1, nbits);
for i=1:nbits
% sample in the middle of the bit
bits(i) = w(floor(i*bitwidth - 0.5*bitwidth));
end
% subtract the mean to normalize it about 0
bits = bits - mean(bits);
% quantize into 1s and 0s
qbits = zeros(1,length(bits));
for i=1:length(bits)
if bits(i) > 0
qbits(i) = 1;
else
qbits(i) = 0;
end
end
nbytes = floor(length(qbits)/8);
bytes = zeros(1,nbytes);
j = 1;
% convert the bitstream into a byte stream
for i=1:8:(nbytes*8)
bytes(j) = qbits(i) * 128 + qbits(i+1) * 64 + qbits(i+2) * 32 + qbits(i+3) * 16 + qbits(i+4) * 8 + qbits(i+5) * 4 + qbits(i+6) * 2 + qbits(i+7);
j = j + 1;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment