function K9plot(Z, fs)

if nargin==0
  fs  = 8000; 
  t  = [0:511]/fs;
  z1 = cos(2*pi*125*t);
  z2 = 2*sin(2*pi*250*t);
  z3 = -0.5*cos(2*pi*500*t + 0.35);
  z4 = 0.27*randn(size(z3));
  z  = z1+z2+z3+z4;
  Z = fft(z);
  disp('Sinusoidit (125, 250 ja 500 Hz) sekä kohinaa');
end;

if nargin==1
  fs = 2;
end;

if nargin>=1
  z = real(ifft(Z));
  t = [1:length(z)]/fs;
end;

fs2 = fs/2;

mag      = abs(Z);
magdB    = 20*log10(abs(Z));
mag_sh   = fftshift(mag);
magdB_sh = fftshift(magdB);

M        = ceil(length(Z)/2);

w2pi   = 2*pi*([0 : length(Z)-1]/length(Z));
w_pipi = 2*pi*([0 : length(Z)-1]/length(Z)) - pi;
wpi    = w2pi(1:M);

figure,

% x-axis linear, y-axis linear
subplot(3,4,1);
plot(w2pi, mag);
set(gca,'XLim',[0 2*pi]);
ylabel('linear');
xlabel('0..2\pi');
%text(12,700,'TITLE');
grid on;

subplot(3,4,2);
plot(w_pipi, mag_sh);
set(gca,'XLim',[-pi pi]);
xlabel('-\pi..\pi');
grid on;

subplot(3,4,3);
plot(wpi, mag(1:M));
set(gca,'XLim',[0 pi]);
xlabel('0..\pi');
grid on;

subplot(3,4,4);
plot(fs2*wpi/pi, mag(1:M));
set(gca,'XLim',[0 fs2]);
xlabel('0..fs/2');
grid on;

% x-axis linear, y-axis logaritmic
subplot(3,4,5);
plot(w2pi, magdB);
set(gca,'XLim',[0 2*pi]);
ylabel('dB');
xlabel('0..2\pi');
grid on;

subplot(3,4,6);
plot(w_pipi, magdB_sh);
set(gca,'XLim',[-pi pi]);
xlabel('-\pi..\pi');
grid on;

subplot(3,4,7);
plot(wpi, magdB(1:M));
set(gca,'XLim',[0 pi]);
xlabel('0..\pi');
grid on;

subplot(3,4,8);
plot(fs2*wpi/pi, magdB(1:M));
set(gca,'XLim',[0 fs2]);
xlabel('0..fs/2');
grid on;


% x-axis logaritmic, y-axis logaritmic
subplot(3,4,12);
semilogx(fs2*wpi/pi, magdB(1:M));
set(gca,'XLim',[0 fs2]);
xlabel('0..fs/2');
ylabel('dB');
grid on;


% signaali demossa
if nargin == 0
  subplot(3,4,12);
  set(gca,'XTick', [100 400 1600]);

  sign_axes = axes('Units','pixels', 'position', [50 40 250 100]);
  axes(sign_axes);
  plot(t,z,'b',t,z1,'k',t,z2,'k:',t,z3,'k-.',t,z4,'r');
  set(sign_axes,'Units','normalized');
  %get(sign_axes)
  legend({'z=z_1+z_2+z_3+z_4','z_1 125 Hz','z_2 250 Hz','z_3 500 Hz','z_4 kohina'});
  title(['Signaali, näytteenottotaajuus fs = ' num2str(fs) ' Hz']);
  grid on;
else
  subplot(3,4,12);
  temp  = fs^(1/5);
  dummy = [-7 : 4];
  temp2 = floor(temp ./ (10.^dummy));
  temp3 = min(find(temp2 == 0));
  temp4 = dummy(temp3);
  temp5 = (10^temp4) * (10.^[1 : 8]);
  temp6 = find(temp5 < fs2);
  temp7 = temp5(temp6);
  set(gca,'XTick', temp7);

  sign_axes = axes('Units','pixels', 'position', [50 40 250 100]);
  axes(sign_axes);
  plot(t,z);
  set(sign_axes,'Units','normalized');
  title(['Signaali, näytteenottotaajuus fs = ' num2str(fs) ' Hz']);
  grid on;
end;