-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_snr.m.bak
executable file
·125 lines (107 loc) · 3.65 KB
/
test_snr.m.bak
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
clear all;
close all;
clc;
FLAG_ADD_TIME = 1 ;
FLAG_ADD_FREQ = 0 ;
FLAG_ADD_NOISE = 1 ;
SNR = 50;%4:0.2:12;
freqOffset = 20e3 ; % 10e3;
ppm = 0.2 ; %0.02
numFrames = 1;
samplesPerSymbol= 12;
Ns = samplesPerSymbol ;
symbolsPerFrame = 16 ;
Nf = symbolsPerFrame ;
fSample = 192e6;
%fSample = 32e6;
fSymbol = 16e6 ;
fs = fSample ;
Ts = 1.0/fs ;
%------------------------------------------------------------------------
% Generate and modulate data
%------------------------------------------------------------------------
%txBits = randi([0 1],symbolsPerFrame,1);
err_num = zeros(length(SNR),1) ;
sum_err_num = zeros(length(SNR),1) ;
total_bits = zeros(length(SNR),1) ;
err_rate = zeros(length(SNR),1) ;
for ii = 1:length(SNR)
for jj = 1:1e6 % frame iteration
snr = SNR(ii) ;
chAWGN = comm.AWGNChannel(...
'NoiseMethod','Signal to noise ratio (Eb/No)', ...
'EbNo',snr,...
'SignalPower',1, ...
'SamplesPerSymbol',samplesPerSymbol);
hLen = 5000 ;
headBits = randi([0 1],1,hLen);
tailBits = randi([0 1],1,hLen);
txBits1 = [0 1 0 0 0 1 0 0 1 1 0 0 1 1 0 1] ;
txBits = [headBits txBits1 tailBits] ;
txSymPr = mskmod(txBits,samplesPerSymbol);
%txSym = [txSymPr,zeros(1,2048-192),txSymPr,zeros(1,2048-192),txSymPr,zeros(1,2048-192)]
txSym=txSymPr;
%------------------------------------------------------------------------
% Transmit through channel
%------------------------------------------------------------------------
if FLAG_ADD_NOISE
rxchan = chAWGN(txSym);
else
rxchan = txSym ;
end
%
% Add timing offset
if FLAG_ADD_TIME
Frac = 1000;
timingOffset = round(ppm*Frac) ;
rxSigTimingOff = resample(rxchan,Frac+timingOffset,Frac) ;
% varDelay = dsp.VariableFractionalDelay;
% rxSigTimingOff = varDelay(rxchan,0.2);
stem(real(rxchan(1:192)));
hold on;
stem(real(rxSigTimingOff(1:192)),'r') ;
else
rxSigTimingOff = rxchan ;
end
%
% Add carrier frequency and phase offset
%rxSigCFO = pfo(rxSigTimingOff);
if FLAG_ADD_FREQ
Len = length(rxSigTimingOff) ;
rxSigCFO = rxSigTimingOff.*exp(-1*j*2*pi*freqOffset*[0:Len-1]*Ts) ;
else
rxSigCFO = rxSigTimingOff ;
end
%
% scatterplot(txSym(1:samplesPerSymbol:end));
% scatterplot(rxSigCFO(1:samplesPerSymbol:end)) ;
% Pass the signal through an AWGN channel
shift = 0;
rxSig = [rxSigCFO(shift+1:end),zeros(1,shift)];
%%%get the corr for ppm estimation
for tt=1:70
rxCorr = rxSig((tt-1)*Ns*Nf+1:(tt)*Ns*Nf+0) ;
ppm_est = time_est(rxCorr,Ns) ;
fprintf("ppm estimation is %f\n",ppm_est) ;
end
%
% Save the transmitted signal for plotting
plot_rx = rxSig;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rx_demod=mskdemod(rxSig,12);
[err_num(ii) tt] = biterr(rx_demod(1:end-1),txBits(1:end-1)) ;
sum_err_num(ii) = sum_err_num(ii)+err_num(ii);
total_bits(ii) = total_bits(ii) + length(txBits) ;
fprintf("SNR is %f and FRAME NUMBER is %d\n" , snr , jj ) ;
if( sum_err_num(ii) > 100 ) || (jj>1000) % at least 10-5
break ;
end
end
err_rate(ii) = sum_err_num(ii)/total_bits(ii) ;
ideal_rate(ii)=1/2*erfc(sqrt(10.^(snr/10)/2));
end
semilogy(SNR,err_rate);
hold on ;
semilogy(SNR,ideal_rate,'r');
keyboard ;