Advertisement
Guest User

Untitled

a guest
May 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.28 KB | None | 0 0
  1. close all
  2. clear
  3. clc
  4.  
  5. S2=0;
  6. S1=1;
  7. msg=randi([S2 S1],1,1e6); %generating random binary sequence
  8. SNR=0:2:30;
  9. threshold=5*sqrt(1/10);
  10.  
  11. %Transmitter Side
  12. transmitted_signal=reshape(repmat(msg,10,1),1,[]).*sqrt(1/10);  %reapeat matrix m 10 times then reshaping it to 1*10^7 vector
  13.                                                                                                           %replacing each bit with it's waveform 1 turns to ten (1/sqrt(10)) and 0 turns to ten zeros
  14. Ptx = mean(msg.^2) % power of transmitted sequence
  15.  
  16. [BER_MF,BER_correlator] = calcChannReciev(SNR,transmitted_signal,threshold,msg)
  17.  
  18. %%% plots
  19. semilogy(SNR,BER_MF)  %Plot the BER curve against SNR
  20. xlim([0 30])
  21. title('BER Of Matched Filter VS SNR');
  22. xlabel('SNR from 0dB to 30dB');
  23. ylabel('BER');
  24.  
  25. figure
  26. semilogy(SNR,BER_correlator)  %Plot the BER curve against SNR
  27. xlim([0 30])
  28. title('BER Of Correlator VS SNR');
  29. xlabel('SNR from 0dB to 30dB');
  30. ylabel('BER');
  31. figure
  32. %%  comparing with expermint 1
  33. x=msg;
  34.  SNR = 0:2:30;
  35. BER_simple = applyNoise(SNR,x)
  36. semilogy(SNR,[BER_simple(1:16)'  BER_MF'])%Plot the BER curve against SNR
  37.  legend('Simple detector','Matched filter and Correlator')
  38. xlim([0 30])
  39. title('Comparison between Matched filter and simple detector(BER VS SNR) ');
  40. xlabel('SNR from 0dB to 30dB');
  41. ylabel('BER');
  42.  
  43. function [BER_MF,BER_correlator] = calcChannReciev(SNR,transmitted_signal,threshold,msg)
  44.    
  45.     for i=1:length(SNR)%calculating channel and reciever side for every snr from 0 to 30 dB with 2 dB steps.
  46.    
  47.         %AWGN channel effect
  48.         received_signal=awgn(transmitted_signal,SNR(i),'measured'); %adding AWGN noise
  49.        
  50.         %% Reciever Side
  51.        
  52.         received_signal=reshape(received_signal,10,[]); %converting the received signal vector into a matrix of 10*10^6 to reduce calculations
  53.         % matched filter
  54.         Hmf=ones(10,1);  %Hmf =c.s1  assuming c=1
  55.        
  56.         %convoluting received signal with matched filter 10 sample by 10 sample but
  57.         %getting the non-padding zero convoluted only (middle sample).
  58.         afterMF=conv2(received_signal,Hmf,'valid');
  59.         Decision_of_MF=afterMF>=threshold;%decision for MF circuit
  60.        
  61.         % correlator filter
  62.         g=ones(10,1); %g=s1
  63.         %multiplying received signal with g(n) and summation for every 10 bits
  64.         aftercorrelator=(received_signal'*g)';
  65.         %decision for correlator circuit
  66.         Decision_of_correlator=aftercorrelator>=threshold;
  67.        
  68.        
  69.         % counting number of errors and BER for every SNR
  70.        
  71.         % calculating No error bits and BER for matched filter
  72.         [MF_errors,BER_MF(i)]=biterr(Decision_of_MF,msg);
  73.        
  74.         %calculating No error bits and BER for correlator
  75.         [correlator_errors,BER_correlator(i)]=biterr(Decision_of_correlator,msg);
  76.        
  77.     end
  78. end
  79.  
  80. function BER_simple = applyNoise(SNR,x)
  81.  
  82.     for i=1:length(SNR)
  83.         Rx_sequence=awgn( x, SNR(i),'measured' );%Apply noise to the signal
  84.        
  85.            Rx_new=Rx_sequence>0.5;%Decide whether the Rx_sequence is 1 or 0 by comparing the samples with threshold=1/2
  86.            [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
  87.        end
  88.  
  89. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement