Code Snippets Submitted by David VP
Discrete Wavelet Transform via linear convolution matrix
% ----------------------------------------------------
% Title: Discrete Wavelet Transform
% by means of a linear convolution matrix
%
% Author: David Valencia
% UPIITA IPN
%
% Posted at: http://www.dsprelated.com/showcode/11.php
%
% Description:
% 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
% convolution matrix method (linear convolution)
%
% Dependencies:
%
% formfilter.m
% http://www.dsprelated.com/showcode/12.php
% upsample2.m
% http://www.dsprelated.com/showcode/10.php
%
% Revisions:
% v1.0a Commented and translated in English
%
% ----------------------------------------------------
close all; clear all; clc;
disp('== GENERALIZED MATRICIAL DWT ==')
%% STEP 1 - Base Filters
% Select type of base filters
typeofbasis = 'o';
typbior = 'bior2.2';
typor = 'db3';
% Obtain base filters
if(typeofbasis == 'b')
[Rf,Df] = biorwavf(typbior);
[h0,h1,g0,g1] = biorfilt(Df,Rf);
elseif (typeofbasis == 'o')
[h0,h1,g0,g1] = wfilters(typor);
end;
%% STEP 2 - Parameter configuration
% One can declare or recover an input vector from another
% program, but here an example vector is built for testing.
N = 16; %Number of input samples
x = (1:N)';
L = length(h0); %Base filter lenght
n_stages = 2; %Of the low-pass stage
n_branches = 2^n_stages; %Number of branches
dec_fact = n_branches; %Decimating factor
% L = Basic filters lenght (h0 ó h1)
% N = Input vector length
% n_stages = number of stages, it generates (2^n_stages) branches
hx = formfilter(n_stages, 1, h0, h1);
Lhx = length(hx);
fprintf('\nLinear convolution matrix processing\n');
%% STEP 3: Build analysis matrices
% -------------------------------------------------------------
% High-pass analysis matrix (single scale) [linear convolution]
% As the DWT has a simple high-pass branch, the matrix is easy
% to build with the base filter.
% Append zeros to each side of the vector to use it as a sliding
% part to build the matrix
movil_1 = [zeros(1,N-1),fliplr(h1),zeros(1,N-1)];
lm = length(movil_1);
n_rows = ceil((L+N-1)/2); %Compute the number of rows
W1 = zeros(n_rows,N); % Allocate memory for the high pass matrix
dec_factor = 2; %Decimate factor for the high-pass matrix
% Build matrix (with sequential shifting)
for i = 0:(n_rows-1)
W1(i+1,:) = movil_1(lm-(i*dec_factor)-N+1 : lm-(i*dec_factor)); %Rama pasaaltas
end
disp('High-pass analysis matrix: ');
W1
disp('High-pass branch results');
y1 = W1 * x
% -------------------------------------------------------------
% Low-pass several stage analysis matrix [linear convolution]
% Get an equivalent filter to get its length
hx = formfilter(n_stages, 1, h0, h1);
Lhx = length(hx);
% Compute the number of rows per branch
rowsperfilter = ceil((N+Lhx)/dec_fact);
% Allocate memory for the low-pass matrix
W0 = zeros((n_branches/2)*rowsperfilter,N);
tic; %Start cronometer
% Build low pass filter
for i = 0:(n_branches/2)-1
% Get the equivalent filter depending on the number of stages
if n_stages < 3
hx = formfilter(n_stages, (n_branches/2) - i, h0, h1);
else
hx = formfilter(n_stages, (n_branches/2) - i - 1, h0, h1);
end
% Append zeros to the vector
movil = [zeros(1,N-1),fliplr(hx),zeros(1,N-1)];
lm = length(movil);
% Shift and add the vector to the matrix
for j = 0:rowsperfilter-1
W0(i*rowsperfilter + j + 1,:) = movil(lm-(dec_fact*j)-N + 1 : lm-(dec_fact*j));
end
end
disp('Low-pass many stage analysis matrix: ');
W0
disp('Low-pass filter bank results: ');
y0 = W0*x
%% STEP 4: Recover signal
disp('Recovered signal: ');
xn1 = (W1')*y1;
xn0 = (W0')*y0;
xn = xn1+xn0
%% STEP 5: Compute error
err = (x - xn).^2;
disp('Error :');
err = (sum(err)/N);
disp(err);
fprintf('\nProcessing time = %d seg \n',toc);
fprintf('\nProgram end >>> \n');
Discrete Wavelet Transform via circular convolution matrix
% ----------------------------------------------------
% Title: Discrete Wavelet Transform
% Author: David Valencia
% UPIITA IPN 2010
%
% 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
% convolution matrix method (circular convolution)
%
% Published in: http://www.dsprelated.com/showcode/23.php
%
% Dependencies:
%
% formfilter.m
% http://www.dsprelated.com/showcode/12.php
% upsample2.m
% http://www.dsprelated.com/showcode/10.php
%
% Revisions:
% v1.0a Commented and translated in English
% ----------------------------------------------------
close all; clear all; clc;
disp('== GENERALIZED MATRICIAL DWT ==')
%% STEP 1 - Base Filters
% Select type of base filters
typeofbasis = 'o';
typbior = 'bior2.2';
typor = 'db2';
% Obtain base filters
if(typeofbasis == 'b')
[Rf,Df] = biorwavf(typbior);
[h0,h1,g0,g1] = biorfilt(Df,Rf);
elseif (typeofbasis == 'o')
[h0,h1,g0,g1] = wfilters(typor);
end;
%% STEP 2 - Parameter configuration
% One can declare or recover an input vector from another
% program, but here an example vector is built for testing.
N = 16; %Number of input samples
x = (1:N)';
L = length(h0); %Base filter lenght
n_stages = 2; %Of the low-pass stage
n_branches = 2^n_stages; %Number of branches
dec_fact = n_branches; %Decimating factor
% L = Basic filters lenght (h0 ó h1)
% N = Input vector length
% n_stages = stage quantity, it generates (2^n_stages) branches
fprintf('\nCircular convolution processing\n');
% ###############################################
% Analysis High-Pass 1-branch Circular convolution matrix
hx = formfilter(n_stages, 1, h0, h1);
Lhx = length(hx);
if Lhx>N
beep; beep; beep;
disp('ERROR: Input vector must be longer, increase n');
disp('End of program ### ');
return;
end
movil_1 = [fliplr(h1),zeros(1,N-L),fliplr(h1),zeros(1,N-L)];
lm = length(movil_1);
dec_fact = 2;
rowsperfilter = N/dec_fact; % Square matrix gets decimated
W1 = zeros(rowsperfilter,N); %MALLOC
for i = 0:(N/2)-1
i;
W1(i+1,:) = movil_1(lm-(N)-(i*dec_fact)+1:lm-(i*dec_fact)); %Rama pasaaltas
end
disp('High-pass matrix: ');
W1
y1 = W1*x;
% ###############################################
% Low-Pass n-branch Analyisis Circular convolution matrix
dec_fact = 2^n_stages;
rowsperfilter = N/dec_fact; % Square matrix gets decimated
% Space for a 3-dimensional array is reserved
% Each branch is assigned to a page of that array
W0 = zeros(rowsperfilter, N, n_branches); %MALLOC
Y0 = zeros(rowsperfilter, n_branches); %MALLOC
% Matrix generation
for i = 0:(n_branches/2)-1
if n_stages < 3
hx = formfilter(n_stages, (n_branches/2) - i, h0, h1);
else
hx = formfilter(n_stages, (n_branches/2) - i - 1, h0, h1);
end
Lhx = length(hx);
movil = [fliplr(hx),zeros(1,N-Lhx),fliplr(hx),zeros(1,N-Lhx)];
lm = length(movil);
for j = 0:rowsperfilter-1
W0( j+1 , : , i+1) = movil(lm-(dec_fact*j)-N + 1 : lm-(dec_fact*j));
end
Y0(:,i+1) = W0(:,:,i+1)*x;
end
disp('Low pass several-stage analysis matrix');
W0
%% Low-pass part recovering code
xnm = zeros(N, n_branches/2); %MALLOC
for i=0:(n_branches/2)-1
xnm(:,i+1) = ((W0(:,:,i+1))')*Y0(:,i+1);
end
[n,m] = size(xnm);
xn1 = (W1')*y1; %High-pass part recovering
xn = zeros(n,1); %MALLOC
% Sum between the low-pass and high-pass recovered parts
for i=1:n
xn(i,1) = sum(xnm(i,:)) + xn1(i);
end
disp('Original signal');
disp(x)
disp('Recovered signal');
disp(xn)
% Compute error
err = (x - xn).^2;
disp('Error :');
err = (sum(err)/N);
disp(err)
TMX Transmultiplexer (MATLAB Version)
% 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;
Fourier Trigonometric Series Approximation
%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');
vectors_intr.asm - External Interruption Configuration
*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
DWT filter generator - formfilter.m
% ------------------------------------------------------
% Title: formfilter.m
%
% Author: David Valencia
%
% Institution: UPIITA-IPN 2010
%
% for DSPrelated.com
%
% Published in: http://www.dsprelated.com/showcode/12.php
%
% Description: This program receives 2 basic ortogonal
% filters (one is high-pass and one is low-pass)
% the filtering level and the branch number.
% As a result it returns the equivalent filter
% of the specified branch.
%
% Dependencies: upsample2.m
% http://www.dsprelated.com/showcode/10.php
%
% Revision: v1.0a
% - Commented and translated in English
%
% For more details on this code and its implementation
% see the following blog posts:
%
% http://www.dsprelated.com/showarticle/115.php
% http://www.dsprelated.com/showarticle/116.php
%
% ------------------------------------------------------
function [hx] = formfilter(n_stages,branch,h0,h1)
p = branch;
% Seed vector
hx = 0;
hx(1) = 1;
switch n_stages
case 1
% If it is a single stage filter
% the branches are just the base filters
if mod(branch,2) ~= 0
hx = h0;
else
hx = h1;
end
case 2
% For a 2 stage filter bank, the
% equivalent filters are simply
% the convolution between the corresponding
% base filters, and one of them is upsampled
% The use of upsample2 is needed to avoid having
% a large quantitiy of zeros at the end of the vector
% which certainly difficults its usage to build a
% convolution matrix.
switch branch
case 1
hx = conv(h0,upsample2(h0,2));
case 2
hx = conv(h0,upsample2(h1,2));
case 3
hx = conv(h1,upsample2(h0,2));
case 4
hx = conv(h1,upsample2(h1,2));
otherwise
beep;
fprintf('\nFor a 2 stage filter bank there can not be a fifth branch');
end
otherwise
% For a n>2 stages filter bank, a more ellaborated
% process must be made. A series of upsamplings and convolutions
% are needed to get an equivalent vector.
for i=0:n_stages-2
q = floor(p /(2^(n_stages-1-i)));
if (q == 1)
hx = conv(hx,upsample2(h1,2^i));
else
hx = conv(hx,upsample2(h0,2^i));
end
p = mod(p,2^(n_stages-1-i));
end
% Depending on the parity of the branch number, the filter
% goes through a last convolution
t = mod(branch,2);
if(t == 1)
hx = conv(hx,upsample2(h0,2^(n_stages-1)));
else
hx = conv(hx,upsample2(h1,2^(n_stages-1)));
end
end
getsinc.m - TMX Transmultiplexer (MATLAB version)
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
formfiltersdwt.m DWT filter generator adaptor
% By David Valencia
% As posted in DSPRelated.com
% at http://www.dsprelated.com/showcode/45.php
% Used for the DWT chain processing program
function [hx] = formfiltersdwt(n_stages,branch,h0,h1)
if(branch==1)
hx=formfilters(n_stages,branch,h0,h1);
elseif(branch==2)
hx=formfilters(n_stages,2,h0,h1);
else
hx=formfilters(n_stages-branch+2,2,h0,h1);
end
% The code line that is changed in formfilters.m for DWT is:
%p = mod(2^(n_stages-1-i),p);
Interrupt.c - DSK6713 External Interruptions via GPIO
/* 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
}
GPIO.c - Using the GPIO as output
// 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>>
TMX Transmultiplexer (MATLAB Version)
% 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
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
// 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)
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
% 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
/* 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
*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
// 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
%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)
% ----------------------------------------------------
% 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 >>>