Sie sind auf Seite 1von 14

%r = received signal

%rolloff = srrc beta


%desired_user = user 1-3 to be decoded

%decoded_msg = decoded text message


%y = output of equalizer
%function [decoded_msg y] = Rx(r, rolloff, desired_user)

load hard.mat
%real carrier
fc1 = 2e6;
%carrier after sampling
fc2 = 0.3e6;
%carrier after squaring
fc3 = 0.25e6;

symbolPeriod = 6.4e-3;
fs = 850e3;
Ts = 1/fs;

%Input Data
figure(1)
plotspec(r,1/fs);
title("PlotSpec of Input Data")

%BandPass Filter for Carrier Recovery


freqs=[0 fc3 - 10000 fc3-5000 fc3 + 5000 fc3+10000 fs/2]/(fs/2);
amps=[0 0 1 1 0 0];
b1=firpm(250,freqs,amps); % BP filter

%PLL Preprocess squaring


squaredData = r.^2;
%Squared Data
figure(2)
plotspec(squaredData , 1/fs);
title("PlotSpec of Squared Data")

%Filtering around carrier


ybp=conv(b1,squaredData);

%Output of Bandpass Filter


figure(3)
plotspec(ybp , 1/fs);
title("PlotSpec of Squared BP Filtered Data")

1
2
%PLL Preprocessing
fftrBPF=fft(ybp); % spectrum of rBPF
[m,imax]=max(abs(fftrBPF(1:round(end/2)))); % find freq of max
peak
ssf=(0:length(ybp))/(Ts*length(ybp)); % frequency vector
freqS=ssf(imax); % freq at the peak
phasep=angle(fftrBPF(imax)); % phase at the peak
[IR,f]=freqz(b1,1,length(ybp),1/Ts); % freq response of filter
[mi,im]=min(abs(f-freqS)); % freq where peak occurs
phaseBPF=angle(IR(im)); % < of BPF at peak freq
phaseS=mod(phasep-phaseBPF,pi); % estimated angle
time = 0:Ts:Ts*length(r)-Ts;

%PLL
mu1=.1; mu2=.0003; % algorithm stepsizes
f0=fc2; % assumed freq at receiver
lent=length(time);
th1=zeros(1,lent); % initialize estimates
th2=zeros(1,lent);

for k=1:lent-1 % combine top PLL th1


th1(k
+1)=th1(k)+mu1*ybp(k)*sin(4*pi*f0*time(k)+2*th1(k)+phaseBPF);
th2(k
+1)=th2(k)+mu2*ybp(k)*sin(4*pi*f0*time(k)+2*th1(k)+2*th2(k)+phaseBPF);
end

3
%theta plots
figure(4)
plot(th1);
title("Theta 1")
figure(5)
plot(th2);
title("Theta 2")

%Demodulation to baseband

demod = r .* cos(2*pi*fc2.*time + th1 + th2)';

%Demodulated Signal

4
figure(6)
plotspec(demod , 1/fs);
title("Plot Spec of Demodulated Data")

m=5.44; % oversampling factor


l = 4;

%Match Filtering
pulshap=srrc(l,rolloff, m, 0); % srrc pulse shape
matchFiltered = conv(pulshap,demod);

figure(8)
plotspec(matchFiltered , 1/fs);
title("Plot Spec of Match Filtered Data")

5
6
%Decisions
tnow=l*m+1; tau=0;
xs=zeros(1,round(length(matchFiltered)/m)); % initialize
variables
tausave=zeros(1,round(length(matchFiltered)/m)); tausave(1)=tau;
i=0;
mu=3e7; % algorithm stepsize
delta=1e-9; % time for derivative
while tnow<length(matchFiltered)-2*l*m % run iteration
i=i+1;
xs(i)=interpsinc(matchFiltered,tnow+tau,l); % interp at tnow
+tau
x_deltap=interpsinc(matchFiltered,tnow+tau+delta,l); % value to
right
x_deltam=interpsinc(matchFiltered,tnow+tau-delta,l); % value to
left
dx=x_deltap-x_deltam; % numerical derivative
tau=tau+mu*dx*xs(i); % alg update (energy)
tnow=tnow+m; tausave(i)=tau; % save for plotting
end

xs = xs .* sqrt(20)/rms(xs)/2;

figure(9)
title("Constellation Diagram for Timing Recovery ")
% plot results
subplot(2,1,1), plot(xs(1:i-2),'b.') % plot constellation
diagram
title('constellation diagram');
ylabel('estimated symbol values')
subplot(2,1,2), plot(tausave(1:i-2)) % plot
trajectory of tau
ylabel('offset estimates'), xlabel('iterations')

7
marker = 'A0Oh well whatever Nevermind';

markerBinary = letters2pam2('A0Oh well whatever Nevermind');

correlation = xcorr(markerBinary, xs);

figure(13)
%[m,ind]=maxk(correlation,12); % location
of largest correlation
[m,ind]=findpeaks(correlation, 'Npeak', 11, 'MinPeakDistance',
1000, 'MinPeakHeight',800 );

if(isempty(m))
correlation = -correlation;
[m,ind]=findpeaks(correlation, 'Npeak', 11, 'MinPeakDistance',
1000, 'MinPeakHeight',800 );
end

subplot(3,1,1), stem(markerBinary) % plot header


title('Header')
subplot(3,1,2), stem(xs) % plot data sequence
title('Data with embedded header')
subplot(3,1,3), stem(correlation) % plot
correlation
title('Correlation of header with data')

8
headstart=length(xs)-ind+1; % place where header starts
outputEqualized = zeros(1 ,245 + 875);
k=24;
headstart = sort(headstart);
f=zeros(k,1);
m=length(markerBinary); s=markerBinary;
e = zeros(1,m);
eTotal = zeros(1,m);

for n = 1:length(headstart)
r = xs(headstart(n) :headstart(n) + 245 -1);
mu=.003; delta=k/2; % stepsize and delay delta
for i=k+1:m % iterate
rr=r(i:-1:i-k+1)'; % vector of received signal
e(i)=s(i-delta)-rr'*f; % calculate error
f=f+mu*e(i)*rr; % update equalizer
coefficients
end
eTotal = [eTotal e];
z = conv(f,xs(headstart(n):headstart(n) + 245 + 3*875 -1));
outputEqualized = [outputEqualized z];

end

9
figure(10)
plot(outputEqualized,'b.');
title("Output of Equalizer")

correlation = xcorr(markerBinary, outputEqualized);


qmatched=quantalph(outputEqualized,[-3,-1,1,3]);

figure(11)
plot(qmatched, 'b.');
title("Output of Equalizer Quantized")

markerLoc = strfind(qmatched', markerBinary);


string = '';

figure(12)
plot(eTotal.^2);
title("Error of Equalizer")

10
11
figure(15)
%[m,ind]=maxk(correlation,12); % location
of largest correlation
[m,ind]=findpeaks(correlation, 'Npeak', 11, 'MinPeakDistance',
1000, 'MinPeakHeight',800 ); % location of
largest correlation
subplot(3,1,1), stem(markerBinary) % plot header
title('Header')
subplot(3,1,2), stem(qmatched) % plot data sequence
title('Data with embedded header')
subplot(3,1,3), stem(correlation) % plot
correlation
title('Correlation of header with data')

headstart=length(qmatched)-ind+1; % place where header


starts
headstart = sort(headstart);

for n = 1:1:length(headstart)
packet = qmatched(headstart(n) + 245 + (desired_user
-1)*875 :headstart(n) + 245 + (desired_user)*875 -1);
string = [string pam2letters2(packet)];
end

load hard.mat

12
decoded_msg = string
y = outputEqualized;
userDataLength=100;

[a b]=size(decoded_msg);
if b==userDataLength
decoded_msg=decoded_msg';
end
decoded_msg=decoded_msg(:)';

% sync up decoded_msg with m


% (but we can toss the first 5 frames since they're not graded on
those)
cor_vec=conv(double(fliplr(m(desired_user,5*userDataLength
+1:end))),double(decoded_msg));
cor_vec=cor_vec(size(m,2)-5*userDataLength:end);
[junk data_start]=max(cor_vec);

% convert from letters to bits


m2=text2bin(m(desired_user,5*userDataLength+1:end));
decoded=text2bin(decoded_msg(data_start:end));

% append Inf's to the end of student vector if it's too short


N=length(m2);
decoded=[decoded Inf*ones(1,N-length(decoded))];

% convert PAM symbols to bits (assumes Grey encoding)


orig_bits=m2;
student_bits=decoded(1:N);

% calculate number of bit errors


e1=sum(abs(orig_bits-student_bits)>0);

% calculate bit error rate


BER=e1/N;

% calculate grade
if BER<1e-3
grade=100;
elseif BER>0.5
grade=0;
elseif BER>0.25
grade=round(150-300*BER);
else
grade=round(69-10*log10(BER));
end

% output results
disp(['BER: ' num2str(BER)])
disp(['Grade: ' num2str(grade)]);
%end

decoded_msg =

13
'I Kibk It Rovt! o{. I Put My Romt Dovn! t&3 Nor A Put Dmvn
I PuT My FooT Down Ant Then I Make Some Love, I Put My Root Down
Like Sweetie Pie By The Stone Altiance Everbody Knows I'm Known Fn2
Dropping Science I'm Electric Like Dick Hyman I Guess You'd Expect
To Catch The Crew Rhymin' Never Let You Down With The Stereo Sound
So Mike, Get On The Mic And Turn It Out We're Talking Root Down, I
Put My Boot Down And If You Want To Battle Me, You're Putting Loot
Down I Said Root Down, It's Time To Scoot Down I'm A Step Up To The
Mic In My Goose Down Come Up Representing From The Upper West Money
Makin' Putting Me To The Test Sometimes I Feel As Though I've Been
Blessed Because I'm Doing What I Want So I Never Rest Well, I'm Not
Coming Out Goofy Like The Fruit Of The Loom Guys Just Strutting Like
The$Meters With The Look-ka Py Py GCause Downtown Brooklyn Is Where I
Was Born but When The KnkW Is Falling, Then I'm Gone You Might! hink
Ti!4 I'm A FRnaric A Phone Call@Erom Utah And I'm Throwing A '

BER: 0.0074286
Grade: 90

Published with MATLAB® R2018a

14

Das könnte Ihnen auch gefallen