% Signal decimator
%
% -----------------------------------------------------------------------------------
%
% (C) 2017, Krzysztof Sozanski, Digital Signal Processing in Power Electronics Control 
% Circuits, second edition, Springer-Verlag, London, 2017. 
% 
% -----------------------------------------------------------------------------------
%
  clear all;
  M=4; % decimation factor
  fs=50*2^6; % sampling frequency
  fsig=50; % signal frequency
  fb=2.3*fsig; % end of band of interest
  N=2^12; % length of block
  t=(0:N-1)/fs; % time vector
  % coherent frequency of input signal
  fsigk=round(fsig/(fs/N))*fs/N;
  % input signal
  x=sin(2*pi*fsigk*t)+0.5*sin(21*pi*fsigk*t)+...
  0.3*sin(41*pi*fsigk*t); 
  % ------ Filter design -----------------------
  Fg=2*fb/fs;
  [b a]=butter(3,Fg);
  % ------ Decimation --------------------------
  w=filter(b,a,x); % filtering 
  y=w(1,1:M:N); % downsampling 
