Question

I need the code written in Matlab software to send a number of bits using the...

I need the code written in Matlab software to send a number of bits using the Pulse Shape Modulation, and demodulate the signal using the correlation coefficient. The part that i need to modified is the part that is in Bold. The two functions are : function [outSignals,time] = modulation(inBits) and function [outBits] = demodulation(inSignals).

%% Main Function

function runComm_code()

    clear;clc;

  SNRdB = 0:0.5:26;

    nBits = 1e6;

    SNRdBLength = length(SNRdB);

    Pe = ones(1,SNRdBLength);

    clc; disp('Simulation running: 0.0% completed.');

    for i = 1:SNRdBLength

        Pe(i) = transmitReceive(nBits,SNRdB(i));

        clc;fprintf('%s: %.1f%% completed','Simulation running',i/SNRdBLength*100);

    end

    hold on; semilogy(SNRdB,Pe); grid on;

    clc; disp('Simulation finished.');

return

%% Bit Transmission Function

function [Perror,nErrors,TxBits,RxBits] = transmitReceive(nBits,SNRdB)

%     TxBits = [0 1 1 0 1 0 0 1 1 1];

    nBitsMax = 100000;

    nBitsOrig = nBits;

    TxBits = [];

    RxBits =[];

    if(nBits > nBitsMax)

        n = ceil(nBits/nBitsMax);

        nErrorsArr = zeros(1,n);

        for i = 1:n

            if(nBits < nBitsMax)

                [~,nErrorsArr(i),TxBitsTmp,RxBitsTmp] = transmitReceive(nBits,SNRdB);

                TxBits = [TxBits,TxBitsTmp];

                RxBits = [RxBits,RxBitsTmp];

                break;

            else

                [~,nErrorsArr(i),TxBitsTmp,RxBitsTmp] = transmitReceive(nBitsMax,SNRdB);

                TxBits = [TxBits,TxBitsTmp];

                RxBits = [RxBits,RxBitsTmp];

                nBits = nBits - nBitsMax;

            end           

        end

        nErrors = sum(nErrorsArr);

        Perror = nErrors/nBitsOrig;

        return;

    end

       

    TxBits = round(rand(1,nBits));

    TxSignals = modulation(TxBits);

    [RxSignals] = addAWGNoise(TxSignals,SNRdB);

    RxBits = demodulation(RxSignals);

    errorBits = TxBits ~= RxBits;

    nErrors = sum(errorBits);   

    Perror = nErrors/nBits;

   

return

%% Modulation and Demodulation Functions

function [outSignals,time] = modulation(inBits)

    % This code is for On-Off Keying (OOK) modulation

    % You need to substitute this with your own Matlab code according

    % to your assigned modulation scheme.

    pulseWidth = 1e-9;

    timeWindow = 10.5e-9;

    carrierFreq = 4e9;

   

    signalBW = 1.5/pulseWidth;

    osFactor = 1 + carrierFreq/signalBW;

   

   [signal1,time] = pulseSignal(pulseWidth,timeWindow,osFactor);

    signal1 = signal1.*squareSignal(time,carrierFreq);

    signalLength = length(signal1);

    signal0 = zeros(signalLength,1);

   

    inBits = inBits(:)';

    nBits = length(inBits);

    outSignals = zeros(signalLength,nBits);

    col0 = inBits == 0;

    col1 = inBits == 1;

    outSignals(:,col0) = repmat(signal0,1,sum(col0));

    outSignals(:,col1) = repmat(signal1,1,sum(col1));

return

function [outBits] = demodulation(inSignals)

   

    % This code is for On-Off Keying (OOK) modulation

    % You need to substitute this with your own Matlab code according

    % to your assigned modulation scheme.

    SNRmin = 10^(12/10);

    Ps = sum(modulation(1).^2);

    Po = Ps./SNRmin;

   

    Psignals = sum(inSignals.^2);

    outBits = Psignals > Po;   

return

%% Noise Adding Function

function [outSignals,noiseSignals] = addAWGNoise(signals,SNRdB)

    signalsSize = size(signals);

    SNR = 10^(SNRdB/10);

    Ps = max(sum(signals.^2));

    Po = Ps./SNR;

    noiseSignals = sqrt(Po/2).*randn(signalsSize(1),signalsSize(2));

    outSignals = signals+noiseSignals;

return

%% Signal Creation Functions

function [outSignal,time] = pulseSignal(Tp,Tw,osFactor)

    if(nargin == 2), osFactor = 1; end

    if(osFactor < 1), osFactor = 1; end

    B = 1.5/Tp;

    fs = osFactor*2*B;

    Nw = ceil(fs*Tw);

    Np = ceil(fs*Tp);

    outSignal = [0; ones(Np,1); zeros(Nw-Np-1,1)];

    outSignal(end)=0;

    time = (0:length(outSignal)-1)'/fs;

return

function [outSignal] = squareSignal(time,freq)

    tsample = time(2)-time(1);

    tperiod = 1/freq;

    n = ceil(tperiod/tsample);

    npos = ceil(n/2);

    nneg = n - npos;

    Nperiods = ceil(time(end)*freq);

    signalTmp = [ones(npos,1); -1*ones(nneg,1)];

    outSignal = repmat(signalTmp,Nperiods+1,1);

    outSignal = [ 0; outSignal(1:length(time)-1) ];

return

0 0
Add a comment Improve this question Transcribed image text
Answer #1
function [y Bitrate MSE Stepsize QNoise]=pcm(A,fm,fs,n)
%A=amplitute of cosine signal
%fm=frequency of cosine signal
%fs=sampling frequency
%n= number of bits per sample
%MSE=Mean Squar error, QNoise=Quantization Noise
%Example [y Bitrate MSE Stepsize QNoise]=pcm(2,3,20,3)
%If you have any problem or feedback please contact me @
%%===============================================
% NIKESH BAJAJ
% Asst. Prof., Lovely Professional University, India
% Almameter: Aligarh Muslim University, India
%%===============================================
t=0:1/(100*fm):1;
x=A*cos(2*pi*fm*t);
%---Sampling-----
ts=0:1/fs:1;
xs=A*cos(2*pi*fm*ts);
%xs Sampled signal
%--Quantization---
x1=xs+A;
x1=x1/(2*A);
L=(-1+2^n); % Levels
x1=L*x1;
xq=round(x1);
r=xq/L;
r=2*A*r;
r=r-A;
%r quantized signal
%----Encoding---
y=[];
for i=1:length(xq)
d=dec2bin(xq(i),n);
y=[y double(d)-48];
end
%Calculations
MSE=sum((xs-r).^2)/length(x);
Bitrate=n*fs;
Stepsize=2*A/L;
QNoise=((Stepsize)^2)/12;
figure(1)
plot(t,x,'linewidth',2)
title('Sampling')
ylabel('Amplitute')
xlabel('Time t(in sec)')
hold on
stem(ts,xs,'r','linewidth',2)
hold off
legend('Original Signal','Sampled Signal');
figure(2)
stem(ts,x1,'linewidth',2)
title('Quantization')
ylabel('Levels L')
hold on
stem(ts,xq,'r','linewidth',2)
plot(ts,xq,'--r')
plot(t,(x+A)*L/(2*A),'--b')
grid
hold off
legend('Sampled Signal','Quantized Signal');
figure(3)
stairs([y y(length(y))],'linewidth',2)
title('Encoding')
ylabel('Binary Signal')
xlabel('bits')
axis([0 length(y) -1 2])
grid
Add a comment
Know the answer?
Add Answer to:
I need the code written in Matlab software to send a number of bits using the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please answer "b" only. %Example code function plotFS(m); %m = user provided number of terms desired in the Fou...

    Please answer "b" only. %Example code function plotFS(m); %m = user provided number of terms desired in the Fourier series; %this code computes the Fourier series of the function %f(x)=0, for -pi<= x <0, % =cos(x) for 0<= x <pi %generate the interval from -pi to pi with step size h; h = pi/100; xx1=[-pi:h:0]; xx2=[0:h:pi]; xx = [xx1, xx2]; %generate the given function f so that it can be graphed ff = [zeros(size(xx1)), cos(xx2)]; %compute the first partial sum...

  • 1. (9 points) In this Question, we are going to perform DSBSC modulation using MAT- LAB....

    1. (9 points) In this Question, we are going to perform DSBSC modulation using MAT- LAB. The signal we want to use is a speech signal. Here is the block diagram of ths system we want to simulate: Modulation Demodulation ult gt) m(t) x Butterworth LPF mr(t) c(t) Gr(t) Figure 1: DSBSC modulation and demodulation. (a) Since we are working with speech signals, we will choose a sampling frequency that is much larger than the bandwidth of the signals. As...

  • Matlab code assistance - task 1 and 6 I think I am right and just need...

    Matlab code assistance - task 1 and 6 I think I am right and just need to be pointed in the right direction if I should change anything %% Task 1 % create the 3 row vectors shown in Ex 8 using 3 different approaches Ex 8 = using the colon operator and also the linspace function, create the following row vectors -5 -4 -3 -2 -1 5 7 9 8 6 4 % 1. list the values explicitly (square...

  • MATLAB %% T = 1; N = 11; np = 2; dt = 0.001; tmax =...

    MATLAB %% T = 1; N = 11; np = 2; dt = 0.001; tmax = np*T; t = -tmax:dt:tmax; %% Function 1 %the following code was used to create the x(t) function xrange = floor((T/dt)/15); x1 = linspace(0,1,xrange); x2 = x1(end-1:-1:1); x3 = linspace(0,2,2*xrange); x4 = x3(end-1:-1:1); x5 = zeros(1,xrange); x6 = x1; x7 = 2*ones(1,xrange); x8 = 1+x2; x9 = -0.5*ones(1,xrange); x10 = x1/2-0.5; xtemp = [x1 x2 x3 x4 x5 x6 x7 x8 x9 x10]; ztemp =...

  • i need Matlab code asap Write a program that evaluates the function f(x) = tan’x +...

    i need Matlab code asap Write a program that evaluates the function f(x) = tan’x + x - 2 between -27 and 27 in steps of 1/10 and plots the results. Create a function handle for your function, and use function feval to evaluate your function at the specified points. 2. Write a program that locates the zeros of the function f(x) = cos éx -0.25 between 0 and 27. Use the function fzero to actually locate the zeros of...

  • MATLAB code starts here --------- clear T0=2; w0=2*pi/T0; f0=1/T0; Tmax=4; Nmax=15; %--- i=1; for t=-Tmax: .01:Tmax...

    MATLAB code starts here --------- clear T0=2; w0=2*pi/T0; f0=1/T0; Tmax=4; Nmax=15; %--- i=1; for t=-Tmax: .01:Tmax T(i)=t; if t>=(T0/2) while (t>T0/2) t=t-T0; end elseif t<=-(T0/2) while (t<=-T0/2) t=t+T0; end end if abs(t)<=(T0/4) y(i)=1; else y(i)=0; end i=i+1; end plot(T,y),grid, xlabel('Time (sec)'); title('y(t) square wave'); shg disp('Hit return..'); pause %--- a0=1/2; F(1)=0; %dc freq C(1)=a0; for n=1:Nmax a(n)=(2/(n*pi))*sin((n*pi)/2); b(n)=0; C(n+1)=sqrt(a(n)^2+b(n)^2); F(n+1)=n*f0; end stem(F,abs,(C)), grid, title(['Line Spectrum: Harmonics = ' num2str(Nmax)]); xlabel('Freq(Hz)'), ylabel('Cn'), shg disp('Hit return...'); pause %--- yest=a0*ones(1,length(T)); for n=1:Nmax yest=yest+a(n)*cos(2*n*pi*T/T0)+b(n)*sin(2*n*pi*T/T0);...

  • How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0,...

    How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...

  • Need help with MATLAB, what code should be added to print the graph of the trapezoid rule code be...

    Need help with MATLAB, what code should be added to print the graph of the trapezoid rule code below? %%Matlab code for Question 5. syms X y intlx exp(x),0,2); %integration of x*exp(x) fprintf("htlntegration of x"2*exp(x) for [O 3] is %2.5f.\n,y) %Integration using Trapizoidal rule nn [100 1000]; for ii 1:2 a:0; b-3; %Integration limit ns-1:nn(i) integral Values-zeros(size(ns)); ourFunction-@(x) x.*2.*exp(x); for n-ns val(n)-trapizoidal (ourFunction,a,b,nn(i); end fprintf nlntegration of x 2*exp(x) using Trapizoidal rule for [O 3]\n') fprintf('1tTrapizoidal integration value for n-%d...

  • I need help fixing this code in C. It keeps giving me an error saying error:...

    I need help fixing this code in C. It keeps giving me an error saying error: ‘O_RDWD’ undeclared (first use in this function) if ((fd = open(fifo, O_RDWD)) < 0) note: each undeclared identifier is reported only once for each function it appears in. Please fix so it compiles properly. //************************************************************************************************************************************************** #include <fcntl.h> #include <stdio.h> #include <errno.h> #include<stdlib.h> #include <string.h> #include<unistd.h> #include <sys/stat.h> #include <sys/types.h> #define MSGSIZE 63 char *fifo = "fifo"; int main(int argc, char **argv){    int fd;...

  • help me please , By using MATLAB How can I plot the solution of linear diffrential...

    help me please , By using MATLAB How can I plot the solution of linear diffrential system ? I want the code in general case We were unable to transcribe this imageclose all clear MATLAB Code: clc A= [1 0 1; 0 0 0;00-1]; % Coefficient Matrix Xo = [5 76]'; % Initial condition V, D = eig(A); % Get the Eigen values and Eigen vectors % General Solution: X=Sum of {c_i*exp(L_i*t) *V_i % where c_i are constants, L_i are...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT