- improved TX and RX

git-svn-id: http://moon:8086/svn/matlab/trunk@5 801c6759-fa7c-4059-a304-17956f83a07c
This commit is contained in:
2014-07-27 13:44:58 +00:00
parent 2e49c862b8
commit 1d2e8ce618
2 changed files with 35 additions and 137 deletions
+17 -120
View File
@@ -1,126 +1,23 @@
% function ofdm_sync(x, nsym, ngrd) function ofdm_rx(x)
% Example: ofdm_sync(x, 1152, 128)
function ofdm_rx(x, offset, nsym_soll, ngrd)
% Init
fa = 48000;
fc = fa/4;
F1 = 750;
F2 = 2250;
F3 = 3000;
close all; close all;
do_evaluate = 1;
alpha = 0.05;
x = real(x); CP_SIZE = 254;
% Detection FFT_SIZE = 1024;
display('Sync detection'); fa = 48000;
nsym_ist = nsym_soll;
xi = zeros(ngrd, 1);
range_max = 24;
range_min = 4;
range_forget = 0.5;
range = range_max;
err = 0;
k = 0;
kk = 1;
remain = length(x);
progress_ival = 1;
progress_last = -1;
nfft = nsym_soll-ngrd
bin_c = fix(nfft*fc/fa)+1 k_fft = 2*sqrt(FFT_SIZE);
bin_f1 = fix(nfft*(fc+F1)/fa)+1
bin_f2 = fix(nfft*(fc+F2)/fa)+1
bin_f3 = fix(nfft*(fc+F3)/fa)+1
k_fft = 3/sqrt(nfft);
phi_c_last = 0;
phi_f1_last = 0;
phi_f2_last = 0;
phi_f3_last = 0;
while remain > 2*nsym_ist,
xsym = x(offset+k*nsym_ist+1:offset+k*nsym_ist+nsym_ist+range);
[xcor, imax, jmax, xi] = xcc(xsym, nsym_ist, ngrd, -range, range, -range, range, xi);
remain = remain - (length(offset+k*nsym_ist+1:offset+k*nsym_ist+nsym_ist+range));
err = -imax + jmax;
offset = offset + imax;
nsym_ist = max(round(nsym_ist + err), ngrd)
sl_offset(kk) = offset;
sl_err(kk) = err;
sl_nsym_ist(kk) = nsym_ist;
range = min(max(fix(range + abs(err) - range_forget), range_min), range_max);
progress = round(100*(length(x)-remain)/length(x));
if (progress ~= progress_last)
progress_last = progress_last + progress_ival;
progress = progress
end;
if (do_evaluate == 1)
X0 = k_fft*fft(xsym(1:nsym_ist-ngrd), nfft);
phi_c(kk) = (1-alpha)*phi_c_last + alpha*angle(X0(bin_c));
phi_f1(kk) = (1-alpha)*phi_f1_last + alpha*angle(X0(bin_f1));
phi_f2(kk) = (1-alpha)*phi_f2_last + alpha*angle(X0(bin_f2));
phi_f3(kk) = (1-alpha)*phi_f3_last + alpha*angle(X0(bin_f3));
phi_c_last = phi_c(kk);
phi_f1_last = phi_f1(kk);
phi_f2_last = phi_f2(kk);
phi_f3_last = phi_f3(kk);
%pause;
end;
k = k + 1;
kk = kk + 1;
end;
% Result
display('Results:');
offset_stddev = sqrt(var(sl_offset))
err_mean = mean(sl_err)
nsym_ist_mean = mean(nsym_ist)
% Diagrams
figure;
plot(1:length(sl_offset), sl_offset); title('offset'); legend('offset'); grid;
figure; figure;
subplot(2,1,1) Ns = (CP_SIZE + FFT_SIZE);
plot(1:length(sl_err), sl_err); title('error'); legend('error'); grid; Nf = length(x)/Ns;
subplot(2,1,2) for k=1:Nf
plot(1:length(sl_nsym_ist), sl_nsym_ist, 1:length(sl_nsym_ist), nsym_soll*ones(size(sl_nsym_ist))); title('Tracking'); legend('nsym_{ist}', 'nsym_{soll}'); grid; Xs = fft(x((k-1)*Ns+CP_SIZE+1:(k-1)*Ns+CP_SIZE+FFT_SIZE), FFT_SIZE)/k_fft;
mag = abs(Xs(1:FFT_SIZE/8));
figure phase = angle(Xs(1:FFT_SIZE/8));
plot(1:length(phi_c), phi_c, 1:length(phi_f1), phi_f1, 1:length(phi_f2), phi_f2, 1:length(phi_f3), phi_f3); grid; legend('phi_{c}', 'phi_{f1}', 'phi_{f2}', 'phi_{f3}'); title('Phase'); subplot(2, 1, 1)
plot(fa*(0:lge(mag)-1)/FFT_SIZE, mag, '-*'); grid;
% custom function subplot(2, 1, 2)
% Correlation plot(fa*(0:lge(phase)-1)/FFT_SIZE, phase, '-*'); axis([0, fa*(lge(phase)-1)/FFT_SIZE, -pi, +pi]);grid;
function [ccm, imax, jmax, xo] = xcc(x, Ns, Ng, i_low, i_high, j_low, j_high, xi) pause (0.001);
end
Nu = Ns - Ng;
off = length(xi);
xo = x(length(x)-off+1:length(x));
xs = [xi' x']';
ccm = 0;
imax = 0;
jmax = 0;
for i_off=i_low:i_high,
ii = off+i_off+1:off+Ng+i_off;
xm_i = mean(xs(ii));
xi = xs(ii)-xm_i;
var_i = var(xi);
for j_off=j_low:j_high,
jj = off+Nu+j_off+1:off+Nu+Ng+j_off;
xm_j = mean(xs(jj));
xj = xs(jj) - xm_j;
var_j = var(xj);
cc = sum(xi.*xj)./sqrt(var_i*var_j);
if (cc > ccm)
imax = i_off;
jmax = j_off;
ccm = cc;
end;
end;
end;
+18 -17
View File
@@ -2,12 +2,14 @@
function [yv xv] = ofdm_tx(N_frames) function [yv xv] = ofdm_tx(N_frames)
close all; close all;
M_QAM = 4;
RC_ROLLOFF = 0.1;
WITH_RCWIN = 1;
WITH_DATA = 1; WITH_DATA = 1;
WITH_FREQ_REF = 1; WITH_FREQ_REF = 1;
WITH_TIME_REF = 0; WITH_TIME_REF = 1;
M_QAM = 16;
RCWIN_ROLLOFF = 0.1;
% Params % Params
fa = 48000; fa = 48000;
@@ -80,11 +82,11 @@ Ts = Tu+Tg
Ng = round(Tg*fa) Ng = round(Tg*fa)
Nu = round(Tu*fa) Nu = round(Tu*fa)
Ns = round(Ts*fa) Ns = round(Ts*fa)
N_fft = Nu N_fft = Nu;
SubCarrierspacing_HZ = fa/N_fft SubCarrierspacing_HZ = fa/N_fft
% TX window % TX window
[txwin Nt] = txwin_eval(Nu, Ng, RC_ROLLOFF); [rcwin Nt] = txwin_eval(Nu, Ng, RCWIN_ROLLOFF);
% Source % Source
qlut = qamtable(M_QAM); qlut = qamtable(M_QAM);
@@ -102,7 +104,8 @@ for k=1:N_frames
% Data % Data
if WITH_DATA == 1 if WITH_DATA == 1
data_i = c2i(N_fft, ofdm_spec_occ.kmin:1:ofdm_spec_occ.kmax); data_i = c2i(N_fft, ofdm_spec_occ.kmin:1:ofdm_spec_occ.kmax);
X(data_i) = 0.20*qlut(round(M_QAM*rand(1, length(data_i))+0.5)); sym = round(M_QAM*rand(1, length(data_i))+0.5);
X(data_i) = 0.20*qlut(sym);
end; end;
% X(c2i(N_fft,65)) = 0.20*qlut(round(M_qam*rand(1, 1)+0.5)); % X(c2i(N_fft,65)) = 0.20*qlut(round(M_qam*rand(1, 1)+0.5));
@@ -117,13 +120,15 @@ for k=1:N_frames
X(c2i(N_fft, ref_c)) = 0.20*ref_a.*(exp(2*pi*i*ref_p/1024)); X(c2i(N_fft, ref_c)) = 0.20*ref_a.*(exp(2*pi*i*ref_p/1024));
end; end;
% X(1) = 0.2;
% Modulate % Modulate
xu = k_fft*ifft(X, N_fft)'; xu = k_fft*ifft(X, N_fft)';
xs = [xu(Nu-Ng+1:Nu) xu xu(1:Nt)] .* txwin; if WITH_RCWIN == 1
xs(1:Nt) = xs(1:Nt) + xt(1:Nt); xs = [xu(Nu-Ng+1:Nu) xu xu(1:Nt)] .* rcwin;
xt = xs(Ng+Nu+1:Ng+Nu+Nt); xs(1:Nt) = xs(1:Nt) + xt(1:Nt);
xt = xs(Ng+Nu+1:Ng+Nu+Nt);
else
xs = [xu(Nu-Ng+1:Nu) xu];
end;
xv = [xv xs(1:Ng+Nu)]; xv = [xv xs(1:Ng+Nu)];
Xs = fft(xs(Ng+1:Ng+Nu), N_fft)/k_fft; Xs = fft(xs(Ng+1:Ng+Nu), N_fft)/k_fft;
plot(fa*(0:N_fft/8-1)/N_fft, abs(Xs(1:N_fft/8)), '-*'); grid; plot(fa*(0:N_fft/8-1)/N_fft, abs(Xs(1:N_fft/8)), '-*'); grid;
@@ -132,16 +137,12 @@ for k=1:N_frames
end; end;
% Modulate on carrier % Modulate on carrier
yc = 0.5*exp(i*2*pi.*(0:length(xv)-1)*fc/fa) .* filter(hlp,1,xv); yc = 0.5*exp(i*2*pi.*(0:length(xv)-1)*fc/fa) .* xv; %filter(hlp,1,xv);
yv = real(yc) + imag(yc); yv = real(yc) + imag(yc);
xv = xv'; xv = conj(xv');
yv = yv'; yv = yv';
% Output
figure;
freqz(hlp);
wavwrite(0.9*yv,fa,16,'yv.wav') wavwrite(0.9*yv,fa,16,'yv.wav')
wavwrite(0.9*[real(xv) imag(xv)],fa,16,'xv.wav') wavwrite(0.9*[real(xv) imag(xv)],fa,16,'xv.wav')