%
% 		 Course:	Biomedical Signal Processing
% Post-Graduate-Studies:        Biomedical Engineering-BΙM
%				University of Patras
%   		   Date:	17-04-2016
% 
%               Part 04:	Gabor Transform
%               		Wavelet Transform
%



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%    Gabor Transform  
%     
%    dgtreal(f,g,a,M,L) computes the Gabor coefficients (also known as a windowed Fourier transform)
%    [c,Ls]=dgtreal(f,g,a,M,L);
%	f 	Input data
%	g 	Window function.
%	a 	Length of time shift.
%	M 	Number of modulations.
%	L 	Length of transform to do.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

clear all
close all

load ECGx1 ;

fs=360;    % The sampling rate of ECGx1 
a=10;      % Downsampling factor in time
M=200;     % Total number of channels, only 101 will be computed
Ls = length(x) ;
L=dgtlength(Ls,a,M);
gs=firwin('blackman',128);
ga=gabdual(gs,a,M,L);

c=dgtreal(x,ga,a,M);  % analysis

% Visualize the coefficients as a spectrogram
dynrange=90; % 90 dB dynamical range for the plotting
plotdgtreal(c,a,M,fs,dynrange);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%    inverse Gabor Transform  
%    IDGTREAL - Inverse discrete Gabor transform for real-valued signals 
%    f=idgtreal(c,g,a,M,Ls); computes the signal from Gabor coefficients
%    f=idgtreal(c,g,a,M,Ls);
%	c 	Array of coefficients.
%	g 	Window function.
%	a 	Length of time shift.
%	M 	Number of channels.
%	Ls 	length of signal.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

x1= idgtreal(c,gs,a,M,Ls); % synthesis

figure
plot(x)
hold on
plot(x1, 'r') ;





%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%	'haar' 	Haar wavelet
%	'db' 	Daubechies wavelets
%	'sym' 	Symlets
%	'coif' 	Coiflets
%	'bior'	Biorthogonal wavelets
%	'rbio' 	Reverse biorthogonal wavelets
%	'meyr' 	Meyer wavelet
%	'dmey' 	Discrete approximation of Meyer wavelet
%	'gaus' 	Gaussian wavelets
%	'mexh' 	Mexican hat wavelet
%	'morl' 	Morlet wavelet
%	'cgau' 	Complex Gaussian wavelets
%	'shan' 	Shannon wavelets
%	'fbsp' 	Frequency B-Spline wavelets
%	'cmor' 	Complex Morlet wavelets
%	'fk' 	Fejer-Korovkin wavelets
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 


plot(wavfun('db1'));
wfiltinfo('db8');



%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%
%     FWT   Fast Wavelet Transform
%      Usage:  c = fwt(f,w,J);
%               c = fwt(f,w,J,dim);
%               [c,info] = fwt(...);
%     
%       Input parameters:
%             f     : Input data.
%             w     : Wavelet definition.
%             J     : Number of filterbank iterations.
%             dim   : Dimension to along which to apply the transform.
%     
%       Output parameters:
%             c      : Coefficient vector.
%             info   : Transform parameters struct.%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

clear all
close all

load ECGx1 ;
fs=360;    % The sampling rate of ECGx1 

J = 10 ;
[c,info] = fwt(x,'db8',J);
plotwavelets(c,info,fs,'dynrange',90);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 
%
% IFWT   Inverse Fast Wavelet Transform
%       Usage:  x1 = ifwt(c,info)
%               x1 = ifwt(c,w,J,Ls)
%               x1 = ifwt(c,w,J,Ls,dim)
%     
%       Input parameters:
%             c      : Wavelet coefficients.
%             info,w : Transform parameters struct/Wavelet filters definition.
%             J      : Number of filterbank iterations.
%             Ls     : Length of the reconstructed signal.
%             dim    : Dimension to along which to apply the transform.
%     
%       Output parameters:
%             x1     : Reconstructed data.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 

x1 = ifwt(c,'db8',J,length(x));

figure
plot(x)
hold on
plot(x1, 'r') ;


