Canonical huffman coding
Show older comments
What are the algorithms available to effeciently do canonical huffman coding in matlab?
Answers (1)
Walter Roberson
on 9 Mar 2011
0 votes
All of them. Matlab is a general purpose programming language that can be used to implement any deterministic algorithm (that fits within memory.)
2 Comments
Adipuram
about 8 hours ago
Edited: Walter Roberson
about 1 hour ago
1. Use MATLAB to find the modulator output in Binary PAM, if the signal waveforms
s1 (t) and s2 (t) are as shown in figure.
2. Plot these wave forms as points in signal space.
3. Suppose while transmission through channel, the signal was affected with AWGN
with to produce a signal r(t),where r(t) is the
demodulator input, given by r(t) = sm(t)+n(t).and n(t) is AWGN with variances given.
4. Use Correlation receiver and plot the correlator output.
5. Use Matched filter to demodulate r(t) and plot the output.
6. Perform all the above questions for the signals s1 (t) and s2 (t) given below.
1
close all;
clear all;
clc;
t = 0:0.01:1;
figure;
subplot(2,1,1);
s1 = ones(1,length(t));
plot(t,s1);
title('s1(t)');
xlabel('Time');
ylabel('Amplitude');
subplot(2,1,2);
s2 = -ones(1,length(t));
plot(t,s2);
title('s2(t)');
xlabel('Time');
ylabel('Amplitude');
figure;
es1 = sum(s1.*s1)*0.01;
ps1 = s1/sqrt(es1);
subplot(2,1,1);
plot(t,ps1);
title('S1(t)');
c21 = sum(s2.*ps1)*0.01;
d2 = s2 - (c21*ps1);
ed2 = sum(d2.*d2)*0.01;
ps2 = d2/sqrt(ed2);
subplot(2,1,2);
plot(t,ps2);
title('S2(t)');
n1 = randn(1,length(s1))*0;
n2 = randn(1,length(s1))*sqrt(0.1);
n3 = randn(1,length(s1));
n4 = randn(1,length(s1))*sqrt(2);
r1 = s1+n1;
r2 = s1+n2;
r3 = s1+n3;
r4 = s1+n4;
r5 = s2+n1;
r6 = s2+n2;
r7 = s2+n3;
r8 = s2+n4;
figure;
subplot(2,4,1); plot(t,r1); title('Signal 1');
subplot(2,4,2); plot(t,r2); title('Noise 0.1');
subplot(2,4,3); plot(t,r3); title('Noise 1');
subplot(2,4,4); plot(t,r4); title('Noise 2');
subplot(2,4,5); plot(t,r5); title('Signal 2');
subplot(2,4,6); plot(t,r6);
subplot(2,4,7); plot(t,r7);
subplot(2,4,8); plot(t,r8);
figure;
stem([sum(r1.*ps1)*0.01, sum(r2.*ps1)*0.01, ...
sum(r3.*ps1)*0.01, sum(r4.*ps1)*0.01], ...
zeros(1,4));
title('Signal Space Representation');
2
close all;
clear all;
clc;
t1 = 0:0.01:1;
t2 = 1:0.01:2;
t = [t1 t2];
figure;
subplot(2,1,1);
s1 = [ones(1,length(t1)) zeros(1,length(t2))];
plot(t,s1);
title('s1(t)');
xlabel('Time');
ylabel('Amplitude');
subplot(2,1,2);
s2 = [zeros(1,length(t1)) ones(1,length(t2))];
plot(t,s2);
title('s2(t)');
xlabel('Time');
ylabel('Amplitude');
figure;
es1 = sum(s1.*s1)*0.01;
ps1 = s1/sqrt(es1);
subplot(2,1,1);
plot(t,ps1);
title('S1(t)');
c21 = sum(s2.*ps1)*0.01;
d2 = s2-(c21*ps1);
ed2 = sum(d2.*d2)*0.01;
ps2 = d2/sqrt(ed2);
subplot(2,1,2);
plot(t,ps2);
title('S2(t)');
n1 = randn(1,length(s1))*0;
n2 = randn(1,length(s1))*sqrt(0.1);
n3 = randn(1,length(s1));
n4 = randn(1,length(s1))*sqrt(2);
r1 = s1+n1;
r2 = s1+n2;
r3 = s1+n3;
r4 = s1+n4;
r5 = s2+n1;
r6 = s2+n2;
r7 = s2+n3;
r8 = s2+n4;
figure;
subplot(2,4,1); plot(t,r1);
subplot(2,4,2); plot(t,r2);
subplot(2,4,3); plot(t,r3);
subplot(2,4,4); plot(t,r4);
subplot(2,4,5); plot(t,r5);
subplot(2,4,6); plot(t,r6);
subplot(2,4,7); plot(t,r7);
subplot(2,4,8); plot(t,r8);
figure;
stem([sum(r1.*ps1)*0.01, sum(r2.*ps1)*0.01,sum(r3.*ps1)*0.01], zeros(1,3));
title('Signal Space Representation');
3
close all;
clear all;
clc;
t = -1:0.01:1;
figure;
subplot(2,1,1);
s1 = ones(1,length(t));
plot(t,s1);
title('s1(t)');
xlabel('Time');
ylabel('Amplitude');
subplot(2,1,2);
s2 = -ones(1,length(t));
plot(t,s2);
title('s2(t)');
xlabel('Time');
ylabel('Amplitude');
figure;
es1 = sum(s1.*s1)*0.01;
ps1 = s1/sqrt(es1);
subplot(2,1,1);
plot(t,ps1);
title('S1(t)');
c21 = sum(s2.*ps1)*0.01;
d2 = s2-(c21*ps1);
ed2 = sum(d2.*d2)*0.01;
% For antipodal signals, the second signal is
% a negative multiple of the first. Therefore,
% the orthogonal residual is zero.
ps2 = -ps1;
subplot(2,1,2);
plot(t,ps2);
title('S2(t)');
n1 = randn(1,length(s1))*0;
n2 = randn(1,length(s1))*sqrt(0.1);
n3 = randn(1,length(s1));
n4 = randn(1,length(s1))*sqrt(2);
r1 = s1+n1;
r2 = s1+n2;
r3 = s1+n3;
r4 = s1+n4;
r5 = s2+n1;
r6 = s2+n2;
r7 = s2+n3;
r8 = s2+n4;
figure;
subplot(2,4,1); plot(t,r1);
subplot(2,4,2); plot(t,r2);
subplot(2,4,3); plot(t,r3);
subplot(2,4,4); plot(t,r4);
subplot(2,4,5); plot(t,r5);
subplot(2,4,6); plot(t,r6);
subplot(2,4,7); plot(t,r7);
subplot(2,4,8); plot(t,r8);
figure;
stem([sum(r1.*ps1)*0.01, sum(r2.*ps1)*0.01, ...
sum(r3.*ps1)*0.01, sum(r4.*ps1)*0.01], ...
zeros(1,4));
title('Signal Space Representation');
4
close all;
clear all;
clc;
t1 = 0:0.01:1;
t2 = 1:0.01:2;
t = [t1 t2];
figure;
subplot(2,1,1);
s1 = [zeros(1,length(t1)) ones(1,length(t2))];
plot(t,s1);
title('s1(t)');
xlabel('Time');
ylabel('Amplitude');
subplot(2,1,2);
s2 = [ones(1,length(t1)) zeros(1,length(t2))];
plot(t,s2);
title('s2(t)');
xlabel('Time');
ylabel('Amplitude');
figure;
es1 = sum(s1.*s1)*0.01;
ps1 = s1/sqrt(es1);
subplot(2,1,1);
plot(t,ps1);
title('S1(t)');
c21 = sum(s2.*ps1)*0.01;
d2 = s2-(c21*ps1);
ed2 = sum(d2.*d2)*0.01;
ps2 = d2/sqrt(ed2);
subplot(2,1,2);
plot(t,ps2);
title('S2(t)');
n1 = randn(1,length(s1))*0;
n2 = randn(1,length(s1))*sqrt(0.1);
n3 = randn(1,length(s1));
n4 = randn(1,length(s1))*sqrt(2);
r1 = s1+n1;
r2 = s1+n2;
r3 = s1+n3;
r4 = s1+n4;
r5 = s2+n1;
r6 = s2+n2;
r7 = s2+n3;
r8 = s2+n4;
figure;
subplot(2,4,1); plot(t,r1);
subplot(2,4,2); plot(t,r2);
subplot(2,4,3); plot(t,r3);
subplot(2,4,4); plot(t,r4);
subplot(2,4,5); plot(t,r5);
subplot(2,4,6); plot(t,r6);
subplot(2,4,7); plot(t,r7);
subplot(2,4,8); plot(t,r8);
figure;
stem([sum(r1.*ps1)*0.01, sum(r2.*ps1)*0.01, ...
sum(r3.*ps1)*0.01], zeros(1,3));
title('Signal Space Representation');
5
clc;
clear all;
close all;
fs = 100;
dt = 0.01;
t = 0:1/fs:1;
s1 = [0:dt:1 1-dt:-dt:0];
s2 = -[0:dt:1 1-dt:-dt:0];
t = linspace(0,2,length(s1));
figure;
subplot(2,1,1);
plot(t,s1);
title('Signal 1');
subplot(2,1,2);
plot(t,s2);
title('Signal 0');
e1 = sum(s1.^2)*dt;
psi1 = s1/sqrt(e1);
figure;
subplot(2,1,1);
plot(t,psi1);
title('Basis Function 1');
subplot(2,1,2);
stem([-1 1],[0 0]);
title('Signal Space Representation');
figure;
plot(t,ones(size(t)));
title('Impulse');
n1 = randn(1,length(s1))*0;
n2 = randn(1,length(s1))*sqrt(0.1);
n3 = randn(1,length(s1));
n4 = randn(1,length(s1))*sqrt(2);
r1 = s1+n1;
r2 = s1+n2;
r3 = s1+n3;
r4 = s1+n4;
r5 = s2+n1;
r6 = s2+n2;
r7 = s2+n3;
r8 = s2+n4;
% Matched filter impulse response
psi11 = fliplr(psi1);
tnew = -fliplr(t);
figure;
plot(tnew,psi11);
title('Matched Filter Impulse Response');
y1 = conv(r1,psi11)*dt;
y2 = conv(r2,psi11)*dt;
y3 = conv(r3,psi11)*dt;
y4 = conv(r4,psi11)*dt;
y5 = conv(r5,psi11)*dt;
y6 = conv(r6,psi11)*dt;
y7 = conv(r7,psi11)*dt;
y8 = conv(r8,psi11)*dt;
ty = (min(t)+min(tnew)):dt:(max(t)+max(tnew));
% Sample the matched filter output at t = 1
x1 = y1(find(abs(ty-1)==min(abs(ty-1)),1));
x2 = y2(find(abs(ty-1)==min(abs(ty-1)),1));
x3 = y3(find(abs(ty-1)==min(abs(ty-1)),1));
x4 = y4(find(abs(ty-1)==min(abs(ty-1)),1));
x5 = y5(find(abs(ty-1)==min(abs(ty-1)),1));
x6 = y6(find(abs(ty-1)==min(abs(ty-1)),1));
x7 = y7(find(abs(ty-1)==min(abs(ty-1)),1));
x8 = y8(find(abs(ty-1)==min(abs(ty-1)),1));
figure;
subplot(2,3,1);
plot(ty,y1);
title('No Noise Signal 1');
subplot(2,3,2);
plot(ty,y5);
title('No Noise Signal 2');
subplot(2,3,3);
plot(ty,y2);
title('Noise (0.1) Signal 1');
subplot(2,3,4);
plot(ty,y6);
title('Noise (0.1) Signal 2');
subplot(2,3,5);
plot(ty,y3);
title('Noise (1) Signal 1');
subplot(2,3,6);
plot(ty,y7);
title('Noise (1) Signal 2');
figure;
stem([x1 x5],[0 0]);
hold on;
stem([x2 x6],[0 0]);
stem([x3 x7],[0 0]);
stem([x4 x8],[0 0]);
title('Combined Signal Space Representation');
xlabel('Signal Space Coordinate');
ylabel('Amplitude');
6
clc;
clear all;
close all;
fs = 100;
dt = 0.01;
t = 0:dt:2;
% Generate two triangular signals
s1 = zeros(size(t));
s2 = zeros(size(t));
idx1 = (t >= 0 & t <= 1);
idx2 = (t >= 1 & t <= 2);
s1(idx1) = t(idx1);
s1(idx2) = 2-t(idx2);
s2 = -s1;
figure;
subplot(2,1,1);
plot(t,s1);
title('Signal 1');
subplot(2,1,2);
plot(t,s2);
title('Signal 0');
% Basis function
e1 = sum(s1.^2)*dt;
psi1 = s1/sqrt(e1);
figure;
subplot(2,1,1);
plot(t,psi1);
title('Basis Function 1');
subplot(2,1,2);
stem([-1 1],[0 0]);
title('Signal Space Representation');
% Add different noise levels
n1 = zeros(size(s1));
n2 = randn(size(s1))*sqrt(0.1);
n3 = randn(size(s1));
n4 = randn(size(s1))*sqrt(2);
r1 = s1+n1;
r2 = s1+n2;
r3 = s1+n3;
r4 = s1+n4;
r5 = s2+n1;
r6 = s2+n2;
r7 = s2+n3;
r8 = s2+n4;
% Matched filter
h = fliplr(psi1);
y1 = conv(r1,h)*dt;
y2 = conv(r2,h)*dt;
y3 = conv(r3,h)*dt;
y4 = conv(r4,h)*dt;
y5 = conv(r5,h)*dt;
y6 = conv(r6,h)*dt;
y7 = conv(r7,h)*dt;
y8 = conv(r8,h)*dt;
ty = (min(t)+min(t)):dt:(max(t)+max(t));
% Find output samples near the signal endpoint
[~,k] = min(abs(ty-2));
x1 = y1(k);
x2 = y2(k);
x3 = y3(k);
x4 = y4(k);
x5 = y5(k);
x6 = y6(k);
x7 = y7(k);
x8 = y8(k);
figure;
subplot(2,3,1);
plot(t,s1);
title('No Noise Signal 1');
subplot(2,3,2);
plot(t,s2);
title('No Noise Signal 2');
subplot(2,3,3);
stem([x1 x5],[0 0]);
title('No Noise Signal Space Representation');
subplot(2,3,4);
stem([x2 x6],[0 0]);
title('Noise (0.1) Signal Space Representation');
subplot(2,3,5);
stem([x3 x7],[0 0]);
title('Noise (1) Signal Space Representation');
subplot(2,3,6);
stem([x4 x8],[0 0]);
title('Noise (2) Signal Space Representation');
figure;
stem([x1 x2 x3 x4],[0 0 0 0]);
hold on;
stem([x5 x6 x7 x8],[0 0 0 0]);
title('Combined Signal Space Representation');
xlabel('Signal Space Coordinate');
ylabel('Amplitude');
legend('Signal 1','Signal 0');
Walter Roberson
36 minutes ago
Those instructions and that code has nothing to do with the topic of huffman encoding.
Categories
Find more on Source Coding in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


