aboutsummaryrefslogtreecommitdiff
path: root/octave/hf_modem_curves.m
blob: d98d40e0f33b08f6ef12c9b64213fad7cc59bd0d (plain)
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
% hf_modem_curves
% David Rowe Feb 2017
%
% Ideal implementations of a bunch of different HF modems, used to
% generate plots for a blog post.

#{
  [X] ideal AWGN/HF curves
  [X] exp AWGN QPSK curves
  [X] exp AWGN DQPSK curves
  [X] exp HF channel model
  [ ] diversity
  [ ] COHPSK frames
      + would require multiple carriers
      + filtering or OFDM
#}

1;

% Gray coded QPSK modulation function

function symbol = qpsk_mod(two_bits)
    two_bits_decimal = sum(two_bits .* [2 1]); 
    switch(two_bits_decimal)
        case (0) symbol =  1;
        case (1) symbol =  j;
        case (2) symbol = -j;
        case (3) symbol = -1;
    endswitch
endfunction


% Gray coded QPSK demodulation function

function two_bits = qpsk_demod(symbol)
    bit0 = real(symbol*exp(j*pi/4)) < 0;
    bit1 = imag(symbol*exp(j*pi/4)) < 0;
    two_bits = [bit1 bit0];
endfunction


% Rate Rs modem simulation model -------------------------------------------------------

function sim_out = ber_test(sim_in)
    bps     = 2;     % two bits/symbol for QPSK
    Rs      = 50;    % symbol rate (needed for HF model)
    
    verbose = sim_in.verbose;
    EbNovec = sim_in.EbNovec;
    hf_en   = sim_in.hf_en;

    % user can supply number of bits per point to get good results
    % at high Eb/No

    if length(sim_in.nbits) > 1
      nbitsvec = sim_in.nbits;
      nbitsvec += 100 - mod(nbitsvec,100);  % round up to nearest 100
    else
      nbitsvec(1:length(EbNovec)) = sim_in.nbits;
    end

    % init HF model

    if hf_en

      % some typical values

      dopplerSpreadHz = 1.0; path_delay = 1E-3*Rs;

      nsymb = max(nbitsvec)/2;
      spread1 = doppler_spread(dopplerSpreadHz, Rs, nsymb);
      spread2 = doppler_spread(dopplerSpreadHz, Rs, nsymb);
      hf_gain = 1.0/sqrt(var(spread1)+var(spread2));
      % printf("nsymb: %d lspread1: %d\n", nsymb, length(spread1));
    end

    for ne = 1:length(EbNovec)

        % work out noise power -------------

        EbNodB = EbNovec(ne);
        EsNodB = EbNodB + 10*log10(bps);
        EsNo = 10^(EsNodB/10);
        variance = 1/EsNo;
        nbits = nbitsvec(ne);
        nsymb = nbits/bps;

        % modulator ------------------------

        tx_bits = rand(1,nbits) > 0.5;        
        tx_symb = [];
        prev_tx_symb = 1;
        for s=1:nsymb
          atx_symb = qpsk_mod(tx_bits(2*s-1:2*s));
          if sim_in.dqpsk
            atx_symb *= prev_tx_symb;
            prev_tx_symb = atx_symb;
          end
          tx_symb = [tx_symb atx_symb];
        end

        % channel ---------------------------

        rx_symb = tx_symb;

        if hf_en

          % simplified rate Rs simulation model that doesn't include
          % ISI, just freq filtering.  We assume perfect phase estimation
          % so it's just amplitude distortion.

          hf_model1 = hf_model2 = zeros(1, nsymb);
          for s=1:nsymb 
            hf_model1(s) = hf_gain*(spread1(s) + exp(-j*path_delay)*spread2(s));
            hf_model     = abs(hf_model1(s));

            if sim_in.diversity
              % include amplitude information from another frequency in channel model
              w1 = 7*2*pi;
              hf_model2(s) = hf_gain*(spread1(s) + exp(-j*w1*path_delay)*spread2(s));
              hf_model     = 0.5*abs(hf_model1(s)) + 0.5*abs(hf_model2(s));
            end

            rx_symb(s) = rx_symb(s).*hf_model;
          end
        end

        % variance is noise power, which is divided equally between real and
        % imag components of noise

        noise = sqrt(variance*0.5)*(randn(1,nsymb) + j*randn(1,nsymb));
        rx_symb += noise;

        % demodulator ------------------------------------------

        % demodulate rx symbols to bits
 
        rx_bits = [];
        prev_rx_symb = 1;
        for s=1:nsymb
          arx_symb = rx_symb(s);
          if sim_in.dqpsk
            tmp = arx_symb;
            arx_symb *= prev_rx_symb';
            prev_rx_symb = tmp;
          end
          two_bits = qpsk_demod(arx_symb);
          rx_bits = [rx_bits two_bits];
        end
        
        % count errors -----------------------------------------

        error_pattern = xor(tx_bits, rx_bits);
        nerrors = sum(error_pattern);
        bervec(ne) = nerrors/nbits;
        if verbose
          printf("EbNodB: % 3.1f nbits: %5d nerrors: %5d ber: %4.3f\n", EbNodB, nbits, nerrors, bervec(ne));
          if verbose == 2
            figure(2); clf;
            plot(rx_symb*exp(j*pi/4),'+','markersize', 10);
            mx = max(abs(rx_symb));
            axis([-mx mx -mx mx]);
            if sim_in.diversity && sim_in.hf_en 
              figure(3);
              plot(1:nsymb, abs(hf_model1), 1:nsymb, abs(hf_model2), 'linewidth', 2);
            end
          end
        end
    end

    sim_out.bervec = bervec;
endfunction


% -------------------------------------------------------------


function run_single
    sim_in.verbose   = 2;
    sim_in.nbits     = 1000;
    sim_in.EbNovec   = 4;
    sim_in.dqpsk     = 0;
    sim_in.hf_en     = 0;
    sim_in.diversity = 0;

    sim_qpsk = ber_test(sim_in);
endfunction


function run_curves
    max_nbits = 1E5;
    sim_in.verbose = 1;
    sim_in.EbNovec = 0:10;
    sim_in.dqpsk   = 0;
    sim_in.hf_en   = 0;
    sim_in.diversity = 0;

    % AWGN -----------------------------

    ber_awgn_theory = 0.5*erfc(sqrt(10.^(sim_in.EbNovec/10)));
    sim_in.nbits    = min(max_nbits, floor(500 ./ ber_awgn_theory));

    sim_qpsk = ber_test(sim_in);
    sim_in.dqpsk = 1;
    sim_dqpsk = ber_test(sim_in);
       
    % HF -----------------------------

    hf_sim_in = sim_in; hf_sim_in.dqpsk = 0; hf_sim_in.hf_en = 1;
    hf_sim_in.EbNovec = 0:16;

    EbNoLin = 10.^(hf_sim_in.EbNovec/10);
    ber_hf_theory = 0.5.*(1-sqrt(EbNoLin./(EbNoLin+1)));

    hf_sim_in.nbits = min(max_nbits, floor(500 ./ ber_hf_theory));
    sim_qpsk_hf = ber_test(hf_sim_in);

    hf_sim_in.dqpsk = 1; 
    sim_dqpsk_hf = ber_test(hf_sim_in);

    hf_sim_in.dqpsk = 0; 
    hf_sim_in.diversity = 1;
    sim_qpsk_hf_div = ber_test(hf_sim_in);

    % Plot results --------------------

    close all;
    figure (1, 'position', [100, 10, 600, 400]); clf;

    semilogy(sim_in.EbNovec, ber_awgn_theory,'r+-;QPSK AWGN theory;', 'linewidth', 2)
    xlabel('Eb/No (dB)')
    ylabel('BER')
    grid("minor")
    axis([min(sim_in.EbNovec) max(sim_in.EbNovec) 1E-3 1])
    hold on;
    
    semilogy([0 4 4], [ber_awgn_theory(5) ber_awgn_theory(5) 1E-3],'k--', 'linewidth', 2);
    hold off;

    figure (2, 'position', [300, 10, 600, 400]); clf;
    semilogy(sim_in.EbNovec, ber_awgn_theory,'r+-;QPSK AWGN theory;','markersize', 10, 'linewidth', 2)
    hold on;
    semilogy(sim_in.EbNovec, sim_qpsk.bervec,'g+-;QPSK AWGN simulated;','markersize', 10, 'linewidth', 2)
    semilogy(sim_in.EbNovec, sim_dqpsk.bervec,'b+-;DQPSK AWGN simulated;','markersize', 10, 'linewidth', 2)
    xlabel('Eb/No (dB)')
    ylabel('BER')
    grid("minor")
    axis([min(sim_in.EbNovec) max(sim_in.EbNovec) 1E-3 1])

    figure (3, 'position', [400, 10, 600, 400]); clf;
    semilogy(sim_in.EbNovec, ber_awgn_theory,'r+-;QPSK AWGN theory;','markersize', 10, 'linewidth', 2)
    hold on;
    semilogy(sim_in.EbNovec, sim_qpsk.bervec,'g+-;QPSK AWGN simulated;','markersize', 10, 'linewidth', 2)
    semilogy(sim_in.EbNovec, sim_dqpsk.bervec,'b+-;DQPSK AWGN simulated;','markersize', 10, 'linewidth', 2)
    semilogy(hf_sim_in.EbNovec, ber_hf_theory,'r+-;QPSK HF theory;','markersize', 10, 'linewidth', 2)
    semilogy(hf_sim_in.EbNovec, sim_dqpsk_hf.bervec,'b+-;DQPSK HF simulated;','markersize', 10, 'linewidth', 2)
    semilogy(hf_sim_in.EbNovec, sim_qpsk_hf.bervec,'g+-;QPSK HF simulated;','markersize', 10, 'linewidth', 2)
    semilogy(hf_sim_in.EbNovec, sim_qpsk_hf_div.bervec,'c+-;QPSK Diversity HF simulated;','markersize', 10, 'linewidth', 2)
    hold off;
    xlabel('Eb/No (dB)')
    ylabel('BER')
    grid("minor")
    axis([min(hf_sim_in.EbNovec) max(hf_sim_in.EbNovec) 1E-3 1])

endfunction

% -------------------------------------------------------------

more off;
rand('seed',1); randn('seed', 1);
run_curves
#run_single