Skip to content

Commit

Permalink
Advanced methods for info. retrieval implemented in Matlab
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoDTN committed Mar 15, 2018
0 parents commit 32dbca9
Show file tree
Hide file tree
Showing 22 changed files with 352 additions and 0 deletions.
Binary file added Exercise Book.pdf
Binary file not shown.
Binary file added Foundations of Signal Processing.pdf
Binary file not shown.
Binary file added Fourier and Wavelet Signal Processing.pdf
Binary file not shown.
Binary file added IntroInfoRepp.pdf
Binary file not shown.
Binary file added L1_Introduction_revised+.pdf
Binary file not shown.
Binary file added L2-vector spaces-2_2.pdf
Binary file not shown.
Binary file added L3-vector spaces-2_3.pdf
Binary file not shown.
192 changes: 192 additions & 0 deletions Lab/Lab_1.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
%ADVANCED METHODS FOR INFORMATION REPRESENTATION
%Lab_1

clear all;
close all;
clc;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%1)Given two vectors in R^2, e1=[e1x e1y]' and e2=[e2x e2y]' find the dual
%basis f1=[f1x f1y]' and f2=[f2x f2y]'.
%The biorthogonal basis must satisfy the following conditions:
%
%1) <e1,f1>=[e1x e1y]'*[f1x f1y]=1
%2) <e2,f1>=[e2x e2y]'*[f1x f1y]=0
%3) <e2,f2>=[e2x e2y]'*[f2x f2y]=1
%4) <e1,f2>=[e1x e1y]'*[f2x f2y]=0
%
%Solving the system composed by 1) and 2) i obtain f1:
% e1x*f1x+e1y*f1y=1
% e2x*f1x+e2y*f1y=0
% A*f1=C
%
%Solving 3) and 4) i obtain f2:
%
% e2x*f2x+e2y*f2y=1
% e1x*f2x+e1y*f2y=0
% B*f2=C

%Given base:
e1=[1;0];
e2=[1/2;1];

%Coefficients of the systems:
A=[e1(1) e1(2); e2(1) e2(2)];
B=[e2(1) e2(2); e1(1) e1(2)];
C=[1;0];

%Biorthogonal base:
f1=A\C;
f2=B\C;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%2)If N>=3 i have to evaluate more conditions, in case of N=3 i have nine
%equations:
%
% e1x*f1x+e1y*f1y+e1z*f1z=1
% e2x*f1x+e2y*f1y+e2z*f1z=0
% e3x*f1x+e3y*f1y+e3z*f1z=0
% A*f1=D
%
% e2x*f2x+e2y*f2y+e2z*f2z=1
% e1x*f2x+e1y*f2y+e1z*f2z=0
% e3x*f2x+e3y*f2y+e3z*f2z=0
% B*f2=D
%
% e3x*f3x+e3y*f3y+e3z*f3z=1
% e1x*f3x+e1y*f3y+e1z*f3z=0
% e2x*f3x+e2y*f3y+e2z*f3z=0
% C*f2=D

e1=[1;0;0];
e2=[1/2;1;0];
e3=[0;1/2;1];

A=[e1(1) e1(2) e1(3); e2(1) e2(2) e2(3); e3(1) e3(2) e3(3)];
B=[e2(1) e2(2) e2(3); e1(1) e1(2) e1(3); e3(1) e3(2) e3(3)];
C=[e3(1) e3(2) e3(3); e1(1) e1(2) e1(3); e2(1) e2(2) e2(3)];
D=[1;0;0];

f1_1=A\D;
f2_1=B\D;
f3_1=C\D;

%Note: the method is working properly, in fact it satisfies <ei,yj>=1 for
%all i=j and zero otherwise! For higher N i have to iterate the same
%procedure.



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%3)The analysis consists of determining the coordinate coefficients a of
%the vector x given the basis y1,y2,...

x=[1/4;1/2];
y1=[1;0];
y2=[0;1];
G=[y1'*y1 y2'*y1; y1'*y2 y2'*y2];
T=[x'*y1;x'*y2];
a=G\T;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%4)The synthesis consists of determining the vector x given the
%coefficients c1,c2,... and the basis y1,y1,...

c=[1;1];
y1=[1;0];
y2=[1/2; 1];
x=c(1)*y1+c(2)*y2;



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%5)Uniform quantizer with parameters Nlevels, and bounds +-ext

t=0:0.01:1;
s=sin(2*pi*2*t);
Nlevels=8;
ext=1;
Yq=round(s/ext*(Nlevels/2))/(Nlevels/2)*ext;
set=find(abs(Yq)>ext);
Yq(set)=ext*sign(Yq(set));
figure; plot(t,s); hold on; stem(t,Yq,'r'); title('Effect of quantization over a sinusoid');



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%6)100 random values from the normal distribution with mean=0 and var=10
X1=random('norm',0,10,[100 2])';



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%7)100 random values from the uniform distribution with mean=0 and var=1
X2=random('norm',0,1,[100 4])';



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%8)

%Find the dual basis for the random vectors contained in X2:
DUAL=zeros(size(X2));
for i=1:length(X2)
DUAL(:,i)=biorb([X2(1,i) X2(2,i)],[X2(3,i) X2(4,i)]);
end

%Compute the analysis of the dual basis:
for i=1:length(X2)
a=[DUAL(1,i);DUAL(2,i)];
b=[DUAL(3,i);DUAL(4,i)];
c(:,i)=analysis(X1(:,i),[a b]);
end

%Quantize the dual basis coefficients with 16 levels and range
%abs(max(min(c),max(c))):
ext=max(max(abs(c)));
C=zeros(size(c));
for i=1:length(c)
C(:,i)=quantizer(c(:,i),16,ext);
end



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%9)

%Reconstructed vector:
xrec=synth(C,X2);

%Reconstruction error:
error=zeros(1,length(xrec));
for i=1:length(X2)
error(i)=norm((X1(:,i)-xrec(:,i)).^2,2);
end



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%10)Repeat 9) for each x

E=zeros(size(X2));
for i=1:length(X2)
for j=1:length(X2)
E(i,j)=norm((X1(:,i)-xrec(:,j)),2)^2;
end
end



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%11)

figure; surf(E); shading('interp'); xlabel('x-xrec'); ylabel('x-xrec'); zlabel('error');
title('Square norm of the errors');

%Note: from the graph i can see that higher the error higher is the
%distance between the signals x and the reconstructed ones, where
%the error is low menas instead that the two signals are close.
Binary file added Lab/Lab_1.pdf
Binary file not shown.
100 changes: 100 additions & 0 deletions Lab/Lab_2.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
%ADVANCED METHODS FOR INFORMATION REPRESENTATION
%Lab_2

%NOTES: useful tools
%sptool
%spectrogram
%fft,stft,cwt
%sound(x,1000)
%scale2freq
%wavemenu

clear all;
close all;
clc;

%Load data
load('cardio100.mat');
w=hamming(2048)';

if 1==0
%Compute the fft of the signals
k1=fftshift(fft(heart100,1000));
k2=fftshift(fft(heart100.*w,1000));
k3=fftshift(fft(heart100));
k4=fftshift(fft(heart100.*w));

figure;
subplot(1,4,1); stem(abs(k1)); ylabel('|k1|'); xlabel('k'); title('fft 1000');
subplot(1,4,2); stem(abs(k2)); ylabel('|k2|'); xlabel('k'); title('fft 1000 + win');
subplot(1,4,3); stem(abs(k3)); ylabel('|k3|'); xlabel('k'); title('fft 2048');
subplot(1,4,4); stem(abs(k4)); ylabel('|k4|'); xlabel('k'); title('fft 2048 + win');

%Square the signals and compute the fft
k1sq=fftshift(fft(heart100.^2,1000));
k2sq=fftshift(fft((heart100.*w).^2,1000));
k3sq=fftshift(fft(heart100.^2));
k4sq=fftshift(fft((heart100.*w).^2));

figure;
subplot(1,4,1); stem(abs(k1sq)); ylabel('|k1sq|'); xlabel('k'); title('fft 1000');
subplot(1,4,2); stem(abs(k2sq)); ylabel('|k2sq|'); xlabel('k'); title('fft 1000 + win');
subplot(1,4,3); stem(abs(k3sq)); ylabel('|k3sq|'); xlabel('k'); title('fft 2048');
subplot(1,4,4); stem(abs(k4sq)); ylabel('|k4sq|'); xlabel('k'); title('fft 2048 + win');

%Recover the frequencies of the main harmonics
N1=1000;
N2=2048;
fc=1000;

m1=find(k1==max(k1));
m2=find(k2==max(k2));
m3=find(k3==max(k3));
m4=find(k4==max(k4));

f1=m1*fc/N1;
f2=m2*fc/N1;
f3=m3*fc/N2;
f4=m4*fc/N2;

m1sq=find(k1sq==max(k1sq));
m2sq=find(k2sq==max(k2sq));
m3sq=find(k3sq==max(k3sq));
m4sq=find(k4sq==max(k4sq));

f1sq=m1sq*fc/N1;
f2sq=m2sq*fc/N1;
f3sq=m3sq*fc/N2;
f4sq=m4sq*fc/N2;

%NOTES: The frequency of the heartbeat estimated roughly from the graph is
%around 7.5 Hz (15 peaks in 2.048 s), here instead the frequency of the
%main harmonic is around 500 Hz. In this case the fft is not useful to extract
%information from our peaks: the main harmonic corresponds to the smaller
%variations inside our signal (that have more energy).
%In fact the Fourier transform of a peak is a constant and
%the transform of a comb of delta is still a comb of delta, but here the
%delta are not uniformally spaced in time and moreover they have not the
%same amplitude, so the expected transform it's nor a constant nor a pure
%comb of delta. The frequencial information of the peaks is spread over
%the spectrum and so we are not able to detect their frequency.
end

%Some tests:
% cfreq=centfrq('mexh',10,'plot');
% a=scal2frq(1,'mexh',1);
% b=scal2frq(1,'mexh',.001);
% c=scal2frq(5,'mexh',.001);
%t=0:0.01:1;
%x=sin(2*pi*10*t);
% save('x.mat','x');

%load('x.mat');
%c=cwt(x,1:32,'mexh','glb'); %contourf(c,8);
%h=cwt(heart100,1:32,'mexh','glb');
x=[1 1 1 1];

d=dwt(x,'dmey');
e=cwt(x,1:4,'dmey');
figure; stem(d);
figure; imagesc(e);
Binary file added Lab/Lab_2.pdf
Binary file not shown.
20 changes: 20 additions & 0 deletions Lab/Lab_3.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
%ADVANCED METHODS FOR INFORMATION REPRESENTATION
%Lab_3

clear all;
close all;
clc;

%Load data
load('cardio100.mat');

g0=1/(4*sqrt(2))*[(1+sqrt(3)), (3+sqrt(3)), (3-sqrt(3)), (1-sqrt(3))];
h0=1/(4*sqrt(2))*[(1-sqrt(3)), (3-sqrt(3)), (3+sqrt(3)), (1+sqrt(3))];
g1=1/(4*sqrt(2))*[0, -(1-sqrt(3)), (3-sqrt(3)), -(3+sqrt(3)), (1+sqrt(3))];
h1=1/(4*sqrt(2))*[(1+sqrt(3)), -(3+sqrt(3)), (3-sqrt(3)), -(1-sqrt(3)), 0];

figure;
subplot(2,2,1); stem(g0); title('g_0(n)');
subplot(2,2,2); stem(h0); title('h_0(n)');
subplot(2,2,3); stem(g1); title('g_1(n)');
subplot(2,2,4); stem(h1); title('h_1(n)');
16 changes: 16 additions & 0 deletions Lab/analysis.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function [c] = analysis(x, Y)
%This function determine the coordinate coefficients c of the vector x given
%the basis y1,y2,... stored in Y=[y1 y2 ...] where:
%c=G^-1*T
%G=[<y1,y1> <y2,y1> ... <yn,y1>; <y1,y2> <y2,y2> ... <yn,y2>; ...; <y1,yn> <y2,yn> ... <yn,yn>]
%T=[<x,y1>; <x,y2>; ...; <x,yn>]
G=zeros(size(Y));
T=zeros(size(Y(:,1)));
for i=1:length(Y)
for j=1:length(Y)
G(i,j)=Y(:,j)'*Y(:,i);
end
T(i,:)=x'*Y(:,i);
end
c=G\T;
end
9 changes: 9 additions & 0 deletions Lab/biorb.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function [F] = biorb(e1, e2)
%This function returns a biorthogonal basis from the given vectors e1, e2.
%The result satisfy the conditions on the scalar products <ei,fj>=1 for all
%i=j and zero otherwise.
A=[e1(1) e1(2); e2(1) e2(2)];
B=[e2(1) e2(2); e1(1) e1(2)];
C=[1;0];
F=[A\C; B\C];
end
Binary file added Lab/cardio.mat
Binary file not shown.
Binary file added Lab/cardio100.mat
Binary file not shown.
7 changes: 7 additions & 0 deletions Lab/quantizer.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function [Yq] = quantizer(Y,Nlevels,ext)
%This function returns the quantized version of the vector Y over Nlevels
%and in the range +ext/-ext.
Yq=round(Y/ext*(Nlevels/2))/(Nlevels/2)*ext;
set=find(abs(Yq)>ext);
Yq(set)=ext*sign(Yq(set));
end
8 changes: 8 additions & 0 deletions Lab/synth.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
function [xrec] = synth(C,basis)
%This function returns the synthesis of a vector given a base and the
%coefficients.
xrec=zeros(size(C));
for i=1:length(basis(1,:))
xrec(:,i)=C(1,i)*[basis(1,i) basis(2,i)]+C(2,i)*[basis(3,i) basis(4,i)];
end
end
Binary file added Lab/x.mat
Binary file not shown.
Binary file added Programma del Corso.pdf
Binary file not shown.
Binary file not shown.
Binary file added Wavelets and Subband Coding.pdf
Binary file not shown.

1 comment on commit 32dbca9

@tariku-mantafo
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good

Please sign in to comment.