0% found this document useful (0 votes)
8 views15 pages

MATLAB Code for Signal Processing Techniques

The document contains multiple MATLAB code snippets for various signal processing tasks including BASK generation, Huffman encoding and decoding, NRZ signal generation, BPSK modulation, BFSK signal generation, and entropy calculation. Each code snippet includes user inputs for parameters and generates corresponding plots or outputs. The codes demonstrate fundamental concepts in digital communication and data encoding techniques.

Uploaded by

Prasad Patil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views15 pages

MATLAB Code for Signal Processing Techniques

The document contains multiple MATLAB code snippets for various signal processing tasks including BASK generation, Huffman encoding and decoding, NRZ signal generation, BPSK modulation, BFSK signal generation, and entropy calculation. Each code snippet includes user inputs for parameters and generates corresponding plots or outputs. The codes demonstrate fundamental concepts in digital communication and data encoding techniques.

Uploaded by

Prasad Patil
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Matlab Code-

%BASK Generation

clear all;

close all;

t=0:0.001:1;

fs=input('Enter the frequency of square wave: ');

f1=input('Enter the frequency of first message signal: ');

f2=input('Enter the frequency of second message signal: ');

a=5*(square(2*pi*fs*t));

subplot(3,3,1);

plot(t,a);

xlabel('time');

ylabel('amplitude');

title('square wave');

grid on;

b=2*sin(2*pi*f1*t);

subplot(3,3,2);

plot(t,b);

xlabel('time');

ylabel('amplitude');

title('message signal of low amplitude');

grid on;

c=4*sin(2*pi*f2*t);

subplot(3,3,3);

plot(t,c,'r');

xlabel('time');

ylabel('amplitude');

19 15 19

title('message signal of high amplitude');

grid on;
n=length(a);

for i=1:n

if(a(i)>=1)

bask(i)=b(i);

elseif(a(i)<=1)

bask(i)=c(i);

end

end

subplot(3,3,5);

plot(t,bask,'k',t,a,'r');

xlabel('time');

ylabel('amplitude');

title('Binary Amplitude Shift Key[BASK]');

grid on;
Output :-
Matlab Code-

close all;

clear all;

i=input('Enter no. of elements= ');

p=input('Enter probabilities= ');

sum=0;

for n=1:i

H=sum+(p(n)*log2(1/p(n)));

sum=H;

end

disp('H(X): ');

disp(H);
OUTPUT:
Matlab Code:

%Write a MATLAB based program for encoding and decoding of Huffman code

%(variable length source coding )

clc;

clear all;

close all;

symbol =[1:5]; % Distinct data symbols appearing in sig

p = [0.1 0.1 0.4 .3 .1]; % Probability of each data symbol

[dict,avglen]=huffmandict(symbol,p)

samplecode = dict{5,2} % Codeword for fifth signal value

dict{1,:}

dict{2,:}

dict{3,:}

dict{4,:}

dict{5,:}

hcode = huffmanenco(symbol,dict); % Encode the data.

dhsig = huffmandeco(hcode,dict); % Decode the code.

disp('encoded msg:');

disp(hcode);

disp('decoded msg:');

disp(dhsig);

code_length=length(hcode)

for m=1:5

H=Hx+(p(m)*log2(1/p(m)));

Hx=H;

end

disp('Hx=');

disp(H);

Efficiency=(Hx/avglen)*100
Output:
Matlab Code-

clear all;

close all;

%Nb is the number of bits to be transmitted

T=1;%Bit rate is assumed to be 1 bit/s;

%bits to be transmitted

b=[1 0 1 0 1] %Rb is the bit rate in bits/second

NRZ_out=[];

%Vp is the peak voltage +v of the NRZ waveform

Vp=1;

%Here we encode input bitstream as Bipolar NRZ-L waveform

for index=1:size(b,2)

if b(index)==1

NRZ_out=[NRZ_out ones(1,200)*Vp];

elseif b(index)==0

NRZ_out=[NRZ_out ones(1,200)*(-Vp)];

end

end

%Generated bit stream impulses

figure(1);

stem(b);

xlabel('Time (seconds)-->')

ylabel('Amplitude (volts)-->')

title('Impulses of bits to be transmitted');

figure(2);

plot(NRZ_out);

xlabel('Time (seconds)-->');

ylabel('Amplitude (volts)-->');

title('Generated NRZ signal');


t=0.005:0.005:5;

%Frequency of the carrier

f=5;

%Here we generate the modulated signal by multiplying it with

%carrier (basis function)

Modulated=NRZ_out.*(sqrt(2/T)*cos(2*pi*f*t));

figure;

plot(Modulated);

xlabel('Time (seconds)-->');

ylabel('Amplitude (volts)-->');

title('BPSK Modulated signal');

y=[];

%We begin demodulation by multiplying the received signal again with

%the carrier (basis function)

demodulated=Modulated.*(sqrt(2/T)*cos(2*pi*f*t));

%Here we perform the integration over time period T using trapz

%Integrator is an important part of correlator receiver used here

for i=1:200:size(demodulated,2)

y=[y trapz(t(i:i+199),demodulated(i:i+199))];

end

received=y>0;

figure;

stem(received);

title('Impulses of Received bits');

xlabel('Time (seconds)-->');

ylabel('Amplitude (volts)');
OUTPUT:
Matlab Code:

clc;
clear all;
close all;

fs = 10000; % Sampling frequency


T = 1; % Total duration (1 sec)
t = 0:1/fs:T; % Time vector
data = [1 0 1 1 0 0 1]; % Binary data sequence

f0 = 500; % Frequency for binary '0'


f1 = 1000; % Frequency for binary '1'

bit_duration = T / length(data); % Time per bit

bfsk_signal = []; % Initialize BFSK signal

% Generate BFSK waveform


for i = 1:length(data)
% Time index for current bit
t_bit = (i-1)*bit_duration : 1/fs : i*bit_duration - 1/fs;

% Choose frequency based on bit value


if data(i) == 1
bfsk_signal = [bfsk_signal cos(2*pi*f1*t_bit)];
else
bfsk_signal = [bfsk_signal cos(2*pi*f0*t_bit)];
end
end

% Time vector for BFSK signal


t_bfsk = 0:1/fs:(length(bfsk_signal)-1)/fs;

% Plot BFSK waveform


figure;
plot(t_bfsk, bfsk_signal, 'LineWidth', 0.5);
xlabel('Time (seconds)');
ylabel('Amplitude');
title('Binary Frequency Shift Keying (BFSK) Signal');
grid on;
Output :
Matlab Code :

clc;
clear all;
close all;
% Define the word to encode
word = 'ENTC';
% Get unique symbols (letters) and their probabilities
symbols = unique(word); % Unique letters in the word
n = length(symbols); % Number of unique symbols
% Calculate the probabilities based on the frequency of each letter in the word
p = zeros(1, n);
for i = 1:n
p(i) = sum(word == symbols(i)) / length(word);
end
% Convert symbols to their ASCII values for Huffman encoding
symbols_numeric = double(symbols); % Convert characters to ASCII values

% Create Huffman dictionary


[dict, avglen] = huffmandict(symbols_numeric, p); % Use numeric symbols

% Encode the word using ASCII values


hcode = huffmanenco(double(word), dict); % Encode the word as numeric

% Decode the encoded message


dhsig = huffmandeco(hcode, dict); % Decode

% Display results
disp('Encoded message: ');
disp(hcode');

disp('Decoded message: ');


disp(char(dhsig)); % Convert back to character array for display

% Calculate entropy
H = -sum(p .* log2(p + (p == 0))); % Avoid log2(0)
disp('H = ');
disp(H);

% Calculate efficiency
Efficiency = (H / avglen) * 100;
disp('Efficiency: ');
disp(Efficiency);
Output:

You might also like