TMX Transmultiplexer (MATLAB Version)

David Valencia February 2, 20116 comments Coded in Matlab
% UPIITA IPN 2010
% Procesamiento Digital de Señales
% Grupo 6TM1  Equipo #8
% Team members
%   Rogelio Baeza Nieves
%   Ponce Mosso Catarina
%   Valencia Pesqueira José David

% TMX Transmultiplexer -> Chain processing

close all;clear all;clc;

n_stages = 2;
n_branches = 2^n_stages;
dec_factor = 2^n_stages;
niter = 500;

N = 16;
input = zeros(2^n_stages,N);
for(k = 0:2^n_stages-1)
    input(k+1,:) = (1 + k * N ):((k+1)*N);
end
input;
in_cnt = 1;

typeofbasis = 'o';
typbior = 'bior2.2';
typor = 'db2';

if(typeofbasis == 'b')
    [Rf,Df] = biorwavf(typbior);
    [h0,h1,g0,g1] = biorfilt(Df,Rf);
elseif (typeofbasis == 'o')
    [h0,h1,g0,g1] = wfilters(typor);
end;

L = length(h0);

try
    sinc_factor = getsinc(n_stages,L) - 1;
catch
    disp('Error');
    return
end

for i = 1:1
hx = fliplr(formafiltrosdwpt(n_stages,2^n_stages,h0,h1));
Lhx = length(hx);
H = zeros(2^n_stages, Lhx);
for i=0:(2^n_stages)-1
    H(i+1,:)=fliplr(formafiltrosdwpt(n_stages,2^n_stages-i,h0,h1));
end
La = length(H(1,:));

G = zeros(2^n_stages, Lhx);
for i=0:(2^n_stages)-1
    G(i+1,:)=fliplr(formafiltrosdwpt(n_stages,2^n_stages-i,g0,g1));
end
Ls = length(G(1,:));

double2cfloat(H,'h', G,'g',sinc_factor);

analysisbuffer = zeros(1,La);

y = zeros(2^n_stages,1);

sbuffer = zeros(2^n_stages, Ls);
xnbuff = zeros(2^n_stages,1);
chanbuffer = [0];
outputbuffer = zeros(2^n_stages,1);
end

for i=1:niter %General iteration counter
    
    %% Synthesis stage
    i
    % Vector shifting (CLK/1)
    for j = 1:(2^n_stages)
        sbuffer(j,:) = recorreder(sbuffer(j,:),1);
    end
    
% Input interpolation
    if (mod(i,2^n_stages) == 1)&&(in_cnt<N-1)
        sbuffer(:,1) = input(:,in_cnt);
        in_cnt = in_cnt + 1;
    else
        sbuffer(:,1) = zeros(2^n_stages,1);
    end
    
    disp('inputs: ');
    disp(sbuffer)
    
    xnbuff = zeros(2^n_stages,1);
    for j=0:(2^n_stages)-1
        for k = 1 : Ls %convolution
            xnbuff(j+1,1) = xnbuff(j+1,1) + sbuffer(j+1,k)*G(j+1,k);
        end
    end
    
    chanbuffer = sum(xnbuff)   
    
    %% Analysis stage
    analysisbuffer = recorreder(analysisbuffer,1);    
    analysisbuffer(1)=chanbuffer;
    analysisbuffer;
   
    if (i>sinc_factor && mod(i-sinc_factor,2^n_stages) == 1)
    % If the counter is a multiply of the decimating factor, then compute
        y = zeros(2^n_stages,1);
        for j = 0:(2^n_stages)-1
            for k=1:La %convolución
                y(j+1,1) = y(j+1,1) + analysisbuffer(k)*H(j+1,k);
            end;
        end
        outputbuffer = y;
    end
    input;
    i
    outputbuffer
    pause;
end
  	return;

TMX formafiltrosdwpt.m - Integrated version

David Valencia February 2, 2011 Coded in Matlab
function [hx] = formafiltrosdwpt(n_stages,branch,h0,h1)
p = branch;

% Integrated version for DWPT/TMX filter generation

hx = 0;
hx(1) = 1;

switch n_stages
    case 1
        if mod(branch,2) ~= 0
            hx = h0;
        else
            hx = h1;
        end
    case 2
        switch branch
            case 1
                hx = conv(h0,upsample(h0,2));
            case 2
                hx = conv(h0,upsample(h1,2));
            case 3
                hx = conv(h1,upsample(h0,2));
            case 4
                hx = conv(h1,upsample(h1,2));
            otherwise
                beep;
                fprintf('\nERROR');
        end
        
    otherwise
        
        for i=0:n_stages-2
            q = floor(p /(2^(n_stages-1-i)));
            if (q == 1)
                hx = conv(hx,upsample(h1,2^i));
            else
                hx = conv(hx,upsample(h0,2^i));
            end
            p = mod(p,2^(n_stages-1-i)); %For DWPT and TMX
        end
        
        t = mod(branch,2);
        if(t == 1)
            hx = conv(hx,upsample(h0,2^(n_stages-1)));
        else
            hx = conv(hx,upsample(h1,2^(n_stages-1)));
        end
        
        
end

db2_2stages.h - TMX Filter data in C

David Valencia February 2, 2011 Coded in C
// Beginning of file

#define sinc 3 // sincronization factor
#define N 11 // Filter length
#define M 4 // Number of filters(branches)

float h[M][N]={
{
0.0000e+000,
1.6747e-002,
2.9006e-002,
-7.9247e-002,
1.1274e-001,
-2.9575e-001,
-7.9247e-002,
7.6226e-001,
-2.9575e-001,
-4.0401e-001,
2.3325e-001,
},
{
0.0000e+000,
-6.2500e-002,
-1.0825e-001,
2.9575e-001,
-4.2075e-001,
6.7075e-001,
-4.5425e-001,
2.0425e-001,
-7.9247e-002,
-1.0825e-001,
6.2500e-002,
},
{
0.0000e+000,
-6.2500e-002,
-1.0825e-001,
-1.3726e-001,
-1.7075e-001,
3.5377e-001,
7.2877e-001,
-4.5753e-002,
-5.1226e-001,
-1.0825e-001,
6.2500e-002,
},
{
0.0000e+000,
2.3325e-001,
4.0401e-001,
5.1226e-001,
6.3726e-001,
2.9575e-001,
7.9247e-002,
-1.2260e-002,
-1.3726e-001,
-2.9006e-002,
1.6747e-002,
},
};

float g[M][N]={
{
0.0000e+000,
2.3325e-001,
-4.0401e-001,
-2.9575e-001,
7.6226e-001,
-7.9247e-002,
-2.9575e-001,
1.1274e-001,
-7.9247e-002,
2.9006e-002,
1.6747e-002,
},
{
0.0000e+000,
6.2500e-002,
-1.0825e-001,
-7.9247e-002,
2.0425e-001,
-4.5425e-001,
6.7075e-001,
-4.2075e-001,
2.9575e-001,
-1.0825e-001,
-6.2500e-002,
},
{
0.0000e+000,
6.2500e-002,
-1.0825e-001,
-5.1226e-001,
-4.5753e-002,
7.2877e-001,
3.5377e-001,
-1.7075e-001,
-1.3726e-001,
-1.0825e-001,
-6.2500e-002,
},
{
0.0000e+000,
1.6747e-002,
-2.9006e-002,
-1.3726e-001,
-1.2260e-002,
7.9247e-002,
2.9575e-001,
6.3726e-001,
5.1226e-001,
4.0401e-001,
2.3325e-001,
},
};

// EOF

getsinc.m - TMX Transmultiplexer (MATLAB version)

David Valencia February 2, 2011 Coded in Matlab
function [sf] = getsinc(n_etapas, L)

% Experimentally obtained synchronization factors

if(n_etapas == 1)
    sf = 2;

elseif (n_etapas == 2)
    sf = L;

elseif L == 4    
    if(n_etapas == 3) % 8 branches
        sf = 22;
    elseif(n_etapas == 4) % 16 branches
        sf = 36;
    elseif(n_etapas == 5) % 32 branches
        sf = 82;
    end
    
elseif L == 6
    if n_etapas == 3 % 8 branches
        sf = 20;
    elseif n_etapas == 4 % 16 branches
        sf = 30;
    elseif n_etapas == 5 % 32 branches
        sf = 80;
    end
    
elseif L == 8
    if(n_etapas == 3) % 8 branches
        sf = 26;
    elseif(n_etapas == 4) % 16 branches
        sf = 32;        
    end
    
elseif L == 10
    if(n_etapas == 1)
        sf = 2;
    elseif(n_etapas == 2)
        sf = 10;
    end
end

double2cfloat.m - TMX Filter formatting for C compilers

David Valencia February 2, 2011 Coded in Matlab
% UPIITA IPN 2010
% José David Valencia Pesqueira

% This program is useful to convert numbers in MATLAB's Double
% format to a text file in float format for C compilers

% You should copy the resulting text to a file and save it with .h
% extension

% THIS PROGRAM RECEIVES AN UNIDIMENTIONAL VECTOR OF DOUBLE NUMBERS

% close all; clear all; clc;

function double2cfloat(x , namex, y, namey, sinc_factor)
[m,n] = size(x);
fprintf('// Begin of file\n\n');
fprintf('#define sinc %d // Sincronization factor\n',sinc_factor);
fprintf('#define N %d // Filter length\n',n);
if m~=0
    fprintf('#define M %d // Number of filters(branches)\n\n',m);
    fprintf('float %s[M][N]=',namex);    
else
    fprintf('float %s[N]=',namex);
end
disp('');
disp('{');
for i=0:m-1 %Rows
    if m~=1
        disp('{');
    end
    for j=0:n-1 %columns
        fprintf('%1.4e,\n',x(i+1,j+1));
    end
    if m~=1 
        fprintf('},\n');
    end
end
disp('};');
fprintf('\n\n');

if m~=0
    fprintf('float %s[M][N]=',namey);    
else
    fprintf('float %s[N]=',namey);
end
disp('');
disp('{');
for i=0:m-1 %Rows
    if m~=1
        disp('{');
    end
    for j=0:n-1 %columns
        fprintf('%1.4e,\n',y(i+1,j+1));
    end
    if m~=1 
        fprintf('},\n');
    end
end
disp('};');

fprintf('\n// EOF\n\n');

Interrupt.c - DSK6713 External Interruptions via GPIO

David Valencia February 2, 20111 comment Coded in C for the TI C67x
/* Interruption.c 
JOSE DAVID VALENCIA PESQUEIRA
UPIITA-IPN 
THIS PROGRAM DETECTS AN EXTERNAL INTERRUPTION ON A GPIO PIN*/ 

#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <time.h> 
#include <csl_gpio.h> 
#include <csl_gpiohal.h> 
#include <csl_irq.h> 

/* GLOBAL VARIABLES
The flags are marked as volatile, to tell the compiler
that these can be modified by an external event, avoiding
to put them on registers and rather putting them on RAM
so they're ready for immediate use
*/

volatile int startflag = 0; 

GPIO_Handle gpio_handle;  /* Handle for the GPIO */ 

// GPIO Registers configuration

GPIO_Config gpio_config = {          
	0x00000000,  
	// gpgc = Interruption passtrhrough mode and direct GPIO control mode
	0x0000FFFF, // gpen = All GPIO 0-15 pins enabled
	0x00000000, // gdir = All GPIO pins as inputs
	0x00000000, // gpval = Stores logical level of pins
	0x00000010, // IRQ enable for pin 4
	0x00000010, // Enable Event for pin 4
	0x00000000  // gppol -- default state  
}; 

irq_ext_enable(){ 
	/* First, globally disable interruptions
	in the CSR (Control Status Register).
	Bit 0 is the GIE (Global Interrupt Enable)*/
	CSR = (CSR)&(0xFFFFFFFE); 
	// Enable NMIE bit in the IER (bit 1)
	IER |= 0x00000002; 
	// Enable INT4 bit in the IER (4th bit)
	IER |= 0x00000010; 
	// Finally, globally enable interruptions in the CSR
	CSR |= 0x00000001; 
} 

main() 
{      
	// To configure the GPIO
	gpio_handle = GPIO_open( GPIO_DEV0, GPIO_OPEN_RESET ); 
	GPIO_config(gpio_handle,&gpio_config); 
	irq_ext_enable(); 
	comm_intr();      //init DSK 
	DSK6713_LED_init(); 
	while(startflag == 0);  
	printf(“La interrupción externa encendió el led 3 del dsk6713\n”); 
	while(1) // Infinite While
}    

interrupt void c_int04()           //ISR 
{ 
	startflag = 1; 
	iter = 0; 
	DSK6713_LED_on(0);   //To turn on the led
}

vectors_intr.asm - External Interruption Configuration

David Valencia February 2, 2011 Coded in ASM for the TI C67x
*Vectors_intr.asm Vector file for interrupt INT11 
   .global _vectors     ;global symbols 
   .global _c_int00 
   .global _vector1 
   .global _vector2 
   .global _vector3 
   .global _c_int04     ; símbolo para EXT_INT4 
   .global _vector5 
   .global _vector6 
   .global _vector7 
   .global _vector8 
   .global _vector9    
   .global _vector10  
   .global _c_int11        ;for INT11 
   .global _vector12   
   .global _vector13    
   .global _vector14 
   .global _vector15 
 
   .ref _c_int00        ;entry address 
 
VEC_ENTRY .macro addr      ;macro for ISR 
    STW   B0,*--B15 
    MVKL  addr,B0 
    MVKH  addr,B0 
    B     B0 
    LDW   *B15++,B0 
    NOP   2 
    NOP    
    NOP    
   .endm 
 
_vec_nmi: 
  B    NRP 
  NOP  5 
 
_vec_dummy: 
  B    IRP 
  NOP  5 
 
 .sect ".vecs"        ;aligned IST section 
 .align 1024 
_vectors: 
_vector0:   VEC_ENTRY _c_int00     ;RESET 
_vector1:   VEC_ENTRY _vec_nmi   ;NMI 
_vector2:   VEC_ENTRY _vec_dummy    ;RSVD 
_vector3:   VEC_ENTRY _vec_dummy 
_vector4:   VEC_ENTRY _c_int04    ;INT04 Externa 
_vector5:   VEC_ENTRY _vec_dummy 
_vector6:   VEC_ENTRY _vec_dummy 
_vector7:   VEC_ENTRY _vec_dummy 
_vector8:   VEC_ENTRY _vec_dummy 
_vector9:   VEC_ENTRY _vec_dummy 
_vector10:  VEC_ENTRY _vec_dummy 
_vector11:  VEC_ENTRY _c_int11      ;ISR address 
_vector12:  VEC_ENTRY _vec_dummy 
_vector13:  VEC_ENTRY _vec_dummy 
_vector14:  VEC_ENTRY _vec_dummy 
_vector15:  VEC_ENTRY _vec_dummy

GPIO.c - Using the GPIO as output

David Valencia January 31, 20114 comments Coded in C for the TI C67x
// GPIO.c	UPIITA-IPN 
// JOSE DAVID VALENCIA PESQUEIRA
//
// THIS PROGRAM ALLOWS THE DSP TO SEND A BIT THROUGH A GPIO PIN (PIN 7 IN THIS EXAMPLE)
// TO TURN ON A LED ON

#include <stdio.h>
#include <stdlib.h>
#include <csl_gpio.h>
#include <csl_gpiohal.h>
#include <csl_irq.h>

// GLOBAL VARIABLES
volatile int flag = 1;
volatile int pulse_flag = 1;

// CODE TO DEFINE GPIO HANDLE
GPIO_Handle gpio_handle; 

// GPIO REGISTER CONFIGURATION
GPIO_Config gpio_config = {         
    0x00000000, // gpgc = Interruption passthrough mode
    0x0000FFFF, // gpen = All GPIO 0-15 pins enabled
    0x0000FFFF, // gdir = All GPIO pins as outputs
    0x00000000, // gpval = Saves the logical states of pins
    0x00000000, // gphm All interrupts disabled for IO pins
    0x00000000, // gplm All interrupts for CPU EDMA disabled
    0x00000000  // gppol -- default state */
};

// Function prototypes
void send_edge();
void delay();

// Function definitions
void delay()
{
	// Delay function
	int count, count2;
	for(count = 0; count < 200; count++)
	{
		for(count2 = 0; count2 < 50000; count2++);
	}
}

void send_edge()
{	
    DSK6713_init();  
    // Open and configure the GPIO
    gpio_handle = GPIO_open( GPIO_DEV0, GPIO_OPEN_RESET );
    GPIO_config(gpio_handle,&gpio_config);
    // Send values through the pins
	
    GPIO_pinWrite(gpio_handle,GPIO_PIN7,0);
    delay();
    GPIO_pinWrite(gpio_handle,GPIO_PIN7,1);
    delay();
    GPIO_pinWrite(gpio_handle,GPIO_PIN7,0);
}

main()
{     
	send_edge(); // Configures the pins and sends a rising edge
	printf(“ ¡Felicidades! ¡Has logrado encender el led! ”);
	
}
// End of program>>

Fourier Trigonometric Series Approximation

David Valencia January 31, 2011 Coded in Matlab
%Trigonometric Fourier Series Approximation
%José David Valencia Pesqueira - UPIITA-IPN
%As posted for Dsprelated.com
 
close all; clear all; clc;
syms t k;

fprintf('\n ### FOURIER TRIGONOMETRICAL SERIES###');

fprintf('\nSpecify the range (t1,t2) for the approximation:\n');
t1=input('t1= ');
t2=input('t2= ');

% Getting the period

fprintf('\nIs there a repeating patterin in this range? (Y/N): ');
resp1=input('','s');

switch resp1
    case 'Y'
        fprintf('\nWhat is the period for the function?\n');
        T=input('T= ');
        omega0=(2*pi)/T;
    case 'N'
        T=t2-t1;
        omega0=(2*pi)/T;
    otherwise
        fprintf('\nInvalid Option');
        fprintf('\nEnd of program>>>');
        return
end

fprintf('\nThe approximation will be generated in a trigonometrical series: \n\n');
fprintf('f(t)=a0+a1*cos(omega0*t)+a2*cos(2*omega0*t)+...+b1*sen(omega0*t)+b2*sen(2*omega0*t)');
 
fprintf('\n\nUntil what coefficient ak,bk should I compute? (positive integer) n= ');
n=input('');
if n<=0
    fprintf('Invalid n');
    fprintf('\nProgram Stop >>>');
    return
end

fprintf('\nIs the function defined by parts? (Y/N): ');
resp1=input('','s');

switch resp1
    case 'N'
        fprintf('\nWrite the function in terms of t\nf(t)= ');
        f=input('');
        
        faprox=0;

        a0=(1/T)*int(f,t,t1,(t1+T));
 
        faprox=a0;
 
        for k=1:n
            a(k)=(2/T)*int((f*cos(k*omega0*t)),t,t1,(t1+T));
            faprox=faprox+a(k)*cos(k*omega0*t);
        end
 
        for k=1:n
            b(k)=(2/T)*int((f*sin(k*omega0*t)),t,t1,(t1+T));
            faprox=faprox+b(k)*sin(k*omega0*t);
        end

    % If the function is defined in many parts
    case 'Y'
        fprintf('\nHow many ranges do you want to define (positive integer): ');
        numinterv=input('');
        numinterv=round(numinterv);
        for k=1:numinterv
            fprintf('\n## Range #%d definition',k);
            fprintf('\n Write the range in the form of t(%d)<t<t(%d) : ',k-1,k);
            fprintf('\nt(%d)= ',k-1);
            a(k,1)=input(''); %Initial limits vector
            fprintf('\nt(%d)= ',k);
            a(k,2)=input(''); %Final limit vector
            fprintf('\nThe range # %d has a start in %d and ends in %d',k,a(k),a(k+1));
            fprintf('\nPlease define f(t) for the %d th interval:\nf(t)= ',k);
            ft(k)=input('');
            fprintf('\nEnd of definition of the function f%d',k);
        end
        
        faprox=0;
        a0=0
        for j=1:numinterv
            a0=a0+(1/T)*int(ft(j),t,a(j,1),a(j,2));
        end
 
        faprox=a0;
 
        % ak coefficient
        ak=0;
        for j=1:numinterv
            acumulador=(2/T)*int((f(j)*cos(k*omega0*t)),t,a(j,1),a(j,2));
            ak=ak+acumulador;
        end

        return
        
        fprintf('\n## End of Debug Execution');
        return    
    otherwise
        fprintf('\nEND OF PROGRAM>>');
        return
end

%% Salida de datos
fprintf('\nThe resulting coeficcients are: ');
a0
a
b
 
fprintf('\n Do you wish to plot the graph of f(t) against the approximate series? (Y/N): ');
resp1=input('','s');
 
if resp1=='Y'
    ezplot(f,[t1,t2]);
    hold on
    ezplot(faprox,[t1,t2]);
    grid;
end
 
fprintf('\nEND OF PROGRAM');

Discrete Wavelet Transform - Chain processing (Linear Convolution)

David Valencia December 5, 20101 comment Coded in Matlab
% ----------------------------------------------------
% Title: Discrete Wavelet Transform
% Author: David Valencia
% UPIITA IPN 2010
%
% Posted in DSPrelated.com
% http://www.dsprelated.com/showcode/47.php
%
% Computes the Discrete Wavelet Transform of
% n levels (and its branches) from a n sample input.
% Base filters can be of any lenght.
%
% Generalized DWT filter bank computed via the
% chain processing method (linear convolution)
%
% Dependencies: 
%   formfilters.m - http://www.dsprelated.com/showcode/44.php
%   formfiltersdwt.m - http://www.dsprelated.com/showcode/45.php
%   upsample2.m - http://www.dsprelated.com/showcode/10.php
%   recorreder.m - http://www.dsprelated.com/showcode/43.php
%   getsincdwt.m - http://www.dsprelated.com/showcode/46.php
%
% Revisions:
%       v1.0a Commented and translated in English
% ----------------------------------------------------

close all; clear all; clc;

% ######################################

disp('== CHAIN PROCESSING DWT ==')

% Filter parameters
typeofbasis = 'o';
typbior = 'bior2.2';
typor = 'db2';

if(typeofbasis == 'b')
    [Rf,Df] = biorwavf(typbior);
    [h0,h1,g0,g1] = biorfilt(Df,Rf);
elseif (typeofbasis == 'o')
    [h0,h1,g0,g1] = wfilters(typor);
end;

% Input values, change as needed
N = 16;
x = (1:N);
L = length(h0);

figure(1);
subplot(2,1,1);
stem(x);
xlabel('Original signal');

% Filter bank parameters, change as needed
n_stages = 3; %Of the low-pass part
% Checked for the minimal case: DWT = DWPT n_stages = 1
n_branches = n_stages + 1; %Of the low-pass part
niter = 300; %Number of iterations

% Define input buffer dimensions
hx = formfilters(n_stages, 1, h0, h1);
lhx = length(hx);

% Input buffer vector
inputbuffer = zeros(lhx,1);

% Synthesis input buffer
sbuffer = zeros(n_branches,lhx);

% This vector will store the length of each branch filter
Lfiltros = zeros(n_branches, 1);

% This vector will store the decimate factor for each branch
dec_factors = zeros(n_branches, 1);

% This vector will store the synchronization factor for each branch
sinc_factors = zeros(n_branches, 1);

% Cycle to get the synchronization factors
for j = 0:n_branches-1
    try
        sinc_factors(j+1) = getsincdwt(n_stages, j+1, L);
    catch error
        disp('ERROR: Error, experimentation is needed for this case');
        disp('The results may not be correct');
        % return;
    end
end

%% Filter matrices formation
% This matrix will store each of the branch filters as vectors
H = zeros(n_branches, lhx); %Malloc
G = zeros(n_branches, lhx); %Malloc

for i = 0:n_stages
    
    if i >= n_stages-1
        dec_factors(i+1) = 2^n_stages;
    else
        dec_factors(i+1) = 2^(i+1);
    end
    
    hx = fliplr(formfiltersdwt(n_stages,n_stages+1-i,h0,h1));
    gx = fliplr(formfiltersdwt(n_stages,n_stages+1-i,g0,g1));
    lhx = length(hx);
    lgx = length(gx);
    Lfiltros(i+1) = lhx;
    H(i+1,1:lhx) = hx;
    G(i+1,1:lhx) = gx;
end

% Debug code
disp('Dimensiones de los filtros')
Lfiltros
disp('Factores de diezmado')
dec_factors
disp('Matriz de filtros análisis: ');
H
disp('Matriz de filtros síntesis: ');
G

chanbufftestigo = zeros(n_branches,1);
outputbuffer = zeros(1,niter);

%% MAIN CYCLE
for i = 1:niter %General FOR (for iterations)
    i % Print iteration number
    % Shifting of input buffer (sequentially)
    
    inputbuffer = recorreder(inputbuffer,1);
    if i<=N
        inputbuffer(1)=x(i);
    else
        inputbuffer(1)=0;
    end
    
    inputbuffer %Print buffer (debug)
    
    %% Analyisis stage (h0 and h1)
    % The following cycle will select each of the branches for processing
    % If the iteration counter is a multiply of the decimation factor,
    % the convolution is calculated, otherwise, zeros are sent to the
    % channel
    
    y = zeros(n_branches,1); %Clear the output buffer
    
    for j = 0:n_branches-1
        fprintf('\nBranch: %d, Decimating factor: %d\n',j+1,dec_factors(j+1));
        fprintf('\nFilter length: %d\n',Lfiltros(j+1));
        if mod(i,dec_factors(j+1)) == 1
            fprintf('i = %d is a multiply of the factor: %d\n',i,dec_factors(j+1));
            tempfilt = H(j+1,1:Lfiltros(j+1));
            % If the current iteration (clock cycle) is a multiply of the
            % factor, the convolution is computed
            for k=1:Lfiltros(j+1) %convolution
                y(j+1,1) = y(j+1,1) + inputbuffer(k)*tempfilt(k);
            end;
        end
        disp('Results in the channel');
        chanbuff(:,1) = y %Debug printing
    end
    
    %% Synthesis stage (g0 and g1)
    
    % Shifting and interpolation of the synthesis buffer
    for j = 1:n_branches
        sbuffer(j,:) = recorreder(sbuffer(j,:),1);
        % Interpolation of the synthesis stage inputs
        if mod(i,dec_factors(j)) == 1
            
            fprintf('Inserting sample to the synthesis branch %d with dec_factor %d\n',j,dec_factors(j));
            sbuffer(j,1) = chanbuff(j,1);
        else
            fprintf('Inserting a zero to the synthesis branch %d with dec_factor %d\n',j,dec_factors(j));
            sbuffer(j,1) = 0;
        end
    end
    
    disp('Synthesis buffer matrix');
    sbuffer
    
    xnbuff = zeros(n_branches,1);
    
    for j=0:n_branches-1
        fprintf('Branch: %d , dec_factor = %d',j+1,dec_factors(j+1));
        
        % === BRANCH SYNCHRONIZATION ===
        % The branches (except the two last ones down the bank filter)
        % will only take a part of the vector that corresponds with their
        % channel, taking 'Lx' elements, being Lx the number of coefficient
        % that their filter has. An offset is applied
        
        if j < n_branches - 2
            [m,n] = size(sbuffer);
            tempsinth = sbuffer(j+1,n-Lfiltros(j+1)+1:n) %TESTING
        else % The lowest branches take the whole vector
            tempsinth = sbuffer(j+1,1+sinc_factors(j+1):Lfiltros(j+1)+sinc_factors(j+1))
        end
        
        for k = 1 : Lfiltros(j+1) %Convolution
            xnbuff(j+1,1) = xnbuff(j+1,1) + tempsinth(k)*G(j+1,k);
        end
    end
    
    xnbuff
    outputbuffer(i) = sum(xnbuff);
    disp('Output buffer: ');
    if i > N
        disp(outputbuffer(1,i-N+1:i))
        figure(1);
        subplot(2,1,2);
        stem(outputbuffer(1,i-N+1:i));        
        xlabel('Recovered signal');
    else
        disp(outputbuffer(1,1:N))
        figure(1);
        subplot(2,1,2);
        stem(outputbuffer(1,1:N));
        xlabel('Recovered signal');        
    end
    pause; 
end

% END OF PROGRAM >>>