Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- close all
- clear
- clc
- S2=0;
- S1=1;
- msg=randi([S2 S1],1,1e6); %generating random binary sequence
- SNR=0:2:30;
- threshold=5*sqrt(1/10);
- %Transmitter Side
- transmitted_signal=reshape(repmat(msg,10,1),1,[]).*sqrt(1/10); %reapeat matrix m 10 times then reshaping it to 1*10^7 vector
- %replacing each bit with it's waveform 1 turns to ten (1/sqrt(10)) and 0 turns to ten zeros
- Ptx = mean(msg.^2) % power of transmitted sequence
- [BER_MF,BER_correlator] = calcChannReciev(SNR,transmitted_signal,threshold,msg)
- %%% plots
- semilogy(SNR,BER_MF) %Plot the BER curve against SNR
- xlim([0 30])
- title('BER Of Matched Filter VS SNR');
- xlabel('SNR from 0dB to 30dB');
- ylabel('BER');
- figure
- semilogy(SNR,BER_correlator) %Plot the BER curve against SNR
- xlim([0 30])
- title('BER Of Correlator VS SNR');
- xlabel('SNR from 0dB to 30dB');
- ylabel('BER');
- figure
- %% comparing with expermint 1
- x=msg;
- SNR = 0:2:30;
- BER_simple = applyNoise(SNR,x)
- semilogy(SNR,[BER_simple(1:16)' BER_MF'])%Plot the BER curve against SNR
- legend('Simple detector','Matched filter and Correlator')
- xlim([0 30])
- title('Comparison between Matched filter and simple detector(BER VS SNR) ');
- xlabel('SNR from 0dB to 30dB');
- ylabel('BER');
- function [BER_MF,BER_correlator] = calcChannReciev(SNR,transmitted_signal,threshold,msg)
- for i=1:length(SNR)%calculating channel and reciever side for every snr from 0 to 30 dB with 2 dB steps.
- %AWGN channel effect
- received_signal=awgn(transmitted_signal,SNR(i),'measured'); %adding AWGN noise
- %% Reciever Side
- received_signal=reshape(received_signal,10,[]); %converting the received signal vector into a matrix of 10*10^6 to reduce calculations
- % matched filter
- Hmf=ones(10,1); %Hmf =c.s1 assuming c=1
- %convoluting received signal with matched filter 10 sample by 10 sample but
- %getting the non-padding zero convoluted only (middle sample).
- afterMF=conv2(received_signal,Hmf,'valid');
- Decision_of_MF=afterMF>=threshold;%decision for MF circuit
- % correlator filter
- g=ones(10,1); %g=s1
- %multiplying received signal with g(n) and summation for every 10 bits
- aftercorrelator=(received_signal'*g)';
- %decision for correlator circuit
- Decision_of_correlator=aftercorrelator>=threshold;
- % counting number of errors and BER for every SNR
- % calculating No error bits and BER for matched filter
- [MF_errors,BER_MF(i)]=biterr(Decision_of_MF,msg);
- %calculating No error bits and BER for correlator
- [correlator_errors,BER_correlator(i)]=biterr(Decision_of_correlator,msg);
- end
- end
- function BER_simple = applyNoise(SNR,x)
- for i=1:length(SNR)
- Rx_sequence=awgn( x, SNR(i),'measured' );%Apply noise to the signal
- Rx_new=Rx_sequence>0.5;%Decide whether the Rx_sequence is 1 or 0 by comparing the samples with threshold=1/2
- [simple_errors,BER_simple(i)] = biterr(x,Rx_new);%Compare the original bits with the detected bits and calculate number of errors and the ratio
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement