-
Notifications
You must be signed in to change notification settings - Fork 4
/
MyReceiver.m
90 lines (70 loc) · 3.1 KB
/
MyReceiver.m
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
function [ OutPutBits ] = MyReceiver( input )
% MyReceiver takes in a vector N+K length complex samples with unit average
% power. The output will be a vector of real valued bits (zeros and ones)
% labeled OutPutBits. Value of N will be the same length as transmitter.
% The value of K will depend on the channel and its length is unknown a
% priori.
tic;
%% Load the config file to adjust parameters %%
configuration;
%% Coarse Frequency Estimation %%
freq_estimate = measure_freq_fft(input, COARSE_FFT_SIZE);
input = correct_freq_offset(input, freq_estimate);
fprintf('Coarse Frequency Estimator corrected a %f Hz offset\n', freq_estimate*F_S)
%% Use Sync Symbols to Identify Packets (Time and Phase Synch) %%
% Find the "ideal" sync
sync_symbols = modulate(SYNC_PATTERN, MODULATION, SAMPLES_PER_SYMBOL);
sync_symbols = apply_filt(sync_symbols, PULSE_SHAPE);
sync_size = length(sync_symbols);
% Find each packet and load into all_packets
% Pre-allocate the packet matrix (estimate number of packets based on message size)
num_packets_guess = ceil(length(input)/(sync_size+PACKET_SIZE_SAMPLES)) + 1;
all_packets = zeros(PACKET_SIZE_SAMPLES, num_packets_guess);
packet_count = 1;
[packet, index, rxd_sync] = sync_packet(input, sync_symbols, PACKET_SIZE_SAMPLES, 1, sync_size+PACKET_SIZE_SAMPLES);
%fprintf('End of first packet at %d\n', index);
rxd_sync_syms = rxd_sync';
all_packets(:, packet_count) = packet;
while (index+sync_size+PACKET_SIZE_SAMPLES) <= length(input)
packet_count = packet_count + 1;
[packet, index, rxd_sync] = sync_packet(input, sync_symbols, PACKET_SIZE_SAMPLES, index-sync_size, index+2*sync_size);
%fprintf('End of packet at %d\n', index);
rxd_sync_syms = horzcat(rxd_sync_syms, rxd_sync');
all_packets(:, packet_count) = packet;
end
% Cut off anything extra that we pre-allocated
all_packets = all_packets(:, 1:packet_count);
%% Equalize and Fine Frequency Sync each packet %%
% Pre-allocate all_symbols
all_symbols = zeros(PACKET_SIZE_SAMPLES, packet_count);
all_symbols = [];
for i=1:packet_count;
%% Equalization %%
equalized_packet = equalizer(all_packets(:,i).', rxd_sync_syms(:,i), sync_symbols, sync_size, MODULATION);
%% Fine Frequency Sync %%
all_symbols(:,i) = window_phase_sync(equalized_packet, PHASE_WINDOW, MODULATION, SAMPLES_PER_SYMBOL, PULSE_SHAPE);
end
all_symbols = all_symbols(:).';
%% Demodulation %%
[bits,softbits] = demodulate(all_symbols, MODULATION, SAMPLES_PER_SYMBOL, PULSE_SHAPE);
%% Viterbi Decoding %%
% Perform Viterbi decoding of aconvolutional code with defined rate and
% constraint length
% Hard Decision %
%decoded_bits = viterbi_decode(bits, 1, GENERATING_POLYS, CONSTRAINT_LENGTH);
% Soft Decision %
if CODING
softbits = interleave(softbits, 64); %Assumes coded packet size of 4096 bits
decoded_bits = soft_viterbi_decode(softbits, 1, GENERATING_POLYS, CONSTRAINT_LENGTH);
else
decoded_bits = bits;
end
%% Depacketize %%
if PACKETIZE
packets = reshape(decoded_bits, PACKET_SIZE_BITS, length(decoded_bits)/PACKET_SIZE_BITS);
OutPutBits = unpacketizeBits(packets, HEADER_SIZE_BITS);
else
OutPutBits = decoded_bits;
end
toc;
end