function  [salida]=filtra(x,fs,bandas);

if nargin<3
   disp('-----------------');
   disp('Función filtro');
   disp('Modo de empleo:');
   disp('[salida]=filtra(x,fs,bandas)')
   disp('x =señal');
   disp('fs=frequencia de muestreo');
   disp('bandas=1 -> tercios; bandas=2 -> octavas');
   disp('-----------------');
	return
end

np1=size(x);
if np1(2)>np1(1)
   x=x';
end
   
npold=length(x);
y=x;
np=floor(npold/32)*32;
clear x
x=y(1:np);
clear y;

    

flim=[89.1,178,355,708,1413,2818,5623];
foct=[125,250,500,1000,2000,4000];

if bandas==1
   flim=[89.1,112,141,178,224,282,355,447,562,708,891,1122,1413,1778,2239,2818,3548,4467,5623];
   foct=[100,125,160,200,250,315,400,500,630,800,1000,1250,1600,2000,2500,3150,4000,5000];
end

nb=length(foct);


if bandas==2
  disp('filtrando por octavas'); 
   
for pp=1:2
   f1=flim(4+pp);
   f2=flim(4+pp+1);
   passband = [f1 f2]/(fs/2);
   %ripple = .1;
   N=8;
   [B(pp,:),A(pp,:)] = butter(N, passband);
end

x1=decimate(x,16);
x2=decimate(x,4);

for pp=1:2
   salida(:,pp)=interp(filter(B(pp,:),A(pp,:),x1),16);
end
disp ('listo hasta 250 Hz')

for pp=1:2
   salida(:,pp+2)=interp(filter(B(pp,:),A(pp,:),x2),4);
end
disp ('listo hasta 1000 Hz')


for pp=1:2
   salida(:,pp+4)=filter(B(pp,:),A(pp,:),x);
end
disp ('listo hasta 4000 Hz')

disp ('listo!!!')

else %filtrado en tercios
   
   
  disp('filtrando por tercios'); 
   
for pp=1:3
   f1=flim(15+pp);
   f2=flim(15+pp+1);
   passband = [f1 f2]/(fs/2);
   %ripple = .1;
   N=8;
   [B(pp,:),A(pp,:)] = butter(N, passband);
end

x1=decimate(x,8);
x2=decimate(x,4);

for jj=1:6
   
   divisor=2^(6-jj);
   
   for pp=1:3
      salida(:,3*(jj-1)+pp)=interp(filter(B(pp,:),A(pp,:),decimate(x,divisor)),divisor);
   end
   disp(['listo hasta ' num2str(4000/(divisor)) ' Hz'])
end
disp ('listo!!!')


end  %final del if bandas  

return

%para comprobar!!!

semilogx((1:44100),20*log10(abs(fft(x(1:44100)))),(1:44100),20*log10(abs(fft(y(1:44100,:)))))

