function [y,y1,ycut,yfiltered,fs] = demofilter(n,f1,f2,cutoff)
  %% combines two sines with different frequencies f1 and f2
  %% then cuts the signal at the frequency cutoff and filters the signal
  %% shows the difference
  
% try [y,ycomponent,ycut,yfiltered,fs] = demofilter(2000,213.15,530.12,500); and listen
% to it
%
%
% output y1 = sin(2*pi*f1)
%         y = sin(2*pi*f1) + sin(2*pi*f2)
%        y4 = abgeschnittenes signal
%        y5 = gefiltertes signal

  pkg load signal
  
  if(nargin < 4)
    cutoff = 500;
    if(nargin < 3)
      f2 = 530.12;
      if(nargin < 2)
	f1 = 213.13;
	if(nargin < 1)
	  n = 2000;
	end
      end
    end
  end

  fs = n;
  
  t = linspace(0,1,n+1);
  t(end) = [];

  y1 = sin(2*pi*f1*t);
  y2 = sin(2*pi*f2*t);
  y = (y1 + y2);


  yt = fft(y);
  figure(1)
  
  semilogy(abs( yt(1:floor(2*n/2)) )+1e-16);
  axis([0,2000,0.1,100]);
  title("Fourier Combined Signal")
  % assume that yt given from 0, N/2, -N/2 + 1
  n = length(yt);
  ycutspec = yt;
  ycutspec(cutoff+1:n/2 + 1) = 0.0;
  ycutspec(n/2+2:end-cutoff) = 0.0;


  y3 = ifft(yt);  % original signal
  ycut = ifft(ycutspec);   % cutted signal

  
  figure(2)
  semilogy(abs(ycutspec(1:floor(2*n/2)))+1e-16);
  title("Fourier cut signal");
  axis([0,2000,0.1,2000]);
  
  figure(3)
  semilogy(abs(y - real(y3))+1e-16);
  title("Abs Error y - ifft(y), original signal");
  
  
  figure(4)
  semilogy(abs(y1 -real(ycut))+1e-16);
  title("Abs Error y1 - ifft(ycut), cut signal");

  ycut = real(ycut)
%  figure(5)
%  plot(y1 - real(y4));
%  title("Abs Error y1 - ifft(ycut)");

  [b,a] = butter(5,cutoff/(n));

  y5 = filter(b,a,y);
  for j=1:1,
    yfiltered = filter(b,a,y5);
  end

 
  
  
  figure(5)
  semilogy(abs(fft(yfiltered)));
  title("fft(filtered(y))");

%  figure(6)
%  semilogy(abs(y1 - real(yfiltered)));
%  title("Abs Error y1 - filtered");
 % for the sounds make everything a bit louder

  y = 0.8*y/max(y);
  y1 = 0.8*real(y1)/max(real(y1));
  ycut = 0.8*real(ycut)/max(real(ycut));
  yfiltered = 0.8*yfiltered/max(yfiltered);



  % to listen
  % if you use sound(y,fs) to hear both signals
  %            sound(y1,fs) to hear the original part
  % and then listen to the cutoff version and the filtered version
  % there should be an additional ringing at the purely cutoff
  % you can also look at sound(y1 - ycut,fs) and sound(y1 - yfiltered)
