imp=[1; zeros(200,1)];
out=afilter(imp,-20);


f1=50;
f2=23000;
fs=48000;
T=1;
[sw,isw]=expsweep(f1,f2,T,fs);

figure
specgram(sw,256,fs);

figure
specgram(isw,256,fs);

nges=length(sw)+length(isw);
d=real(ifft(fft(sw,nges).*fft(isw,nges)))/length(sw);

figure;
plot(d);
xlabel('Samples'); ylabel('Amplitude');
figure;
plot(etc(d));
xlabel('Samples'); ylabel('Amplitude (dB)');
figure;
spect(fft(d),fs);
lag=255;
ds=d(length(sw):length(sw)+lag);
figure;
plot(ds);
xlabel('Samples'); ylabel('Amplitude');
figure; 
plot(etc(ds));
xlabel('Samples'); ylabel('Amplitude (dB)');

lag=255;
y=asystem([sw; zeros(lag,1)]);
figure
specgram(y,256,fs);

nges=length(sw)+length(isw)+lag;
h=real(ifft(fft(y,nges).*fft(isw,nges)))/length(sw);
figure;
plot(h);
xlabel('Samples'); ylabel('Amplitude');
figure;
plot(etc(h));
xlabel('Samples'); ylabel('Amplitude (dB)');
figure;
spect(fft(h),fs);
hs=h(length(sw):length(sw)+lag);
figure;
plot(hs);
xlabel('Samples'); ylabel('Amplitude');
figure; 
plot(etc(hs));
xlabel('Samples'); ylabel('Amplitude (dB)');
figure;
spect(fft(hs),fs);


    % system: lin, noise
lag=255;
yn=asystem([sw; zeros(lag,1)],-20);
figure
specgram(yn,256,fs);

nges=length(sw)+length(isw)+lag;
hn=real(ifft(fft(yn,nges).*fft(isw,nges)))/length(sw);
figure;
plot(etc(hn));
hn=hn(length(sw):length(sw)+lag);
figure; 
plot(etc(hn));
figure;
spect(fft(hn),fs);

    % system: lin, nonlin
ynl=asystem([sw; zeros(lag,1)],[], -20);
figure
specgram(ynl,256,fs);

lag=255;
nges=length(sw)+length(isw)+lag;
hnl=real(ifft(fft(ynl,nges).*fft(isw,nges)))/length(sw);

figure;
plot(etc(hnl));
hnls=hnl(length(sw):length(sw)+lag);
figure; 
plot(etc(hnls));
figure;
spect(fft(hnls),fs);

nmax=19;
n=round(T*fs-T*fs*log(1:nmax)/log(f2/f1));
hm=zeros(lag+1,nmax);
hamp=zeros(fs/2,nmax);
for ii=1:nmax
  hm(:,ii) = hnl(n(ii):n(ii)+lag);
  htemp=20*log10(abs(fft(hm(:,ii),fs)));
  hamp(:,ii) = htemp(1:fs/2);
end
clear htemp;

for ii=1:nmax
  figure;
  plot(etc(hm(:,ii)));
  title(['harmonic #' num2str(ii) ]);
  axis([0 255 -100 0]);
end


figure
hold on;
plot(hamp(:,1));
plot(hamp(:,2),'r');
plot(hamp(:,3),'g');
plot(hamp(:,4),'k');
plot(hamp(:,5),'m');
plot(hamp(:,6),'c');
legend('#1','#2','#3','#4','#5','#6');

figure
hold on;
plot(hamp(:,1));
plot(hamp(:,3),'r');
plot(hamp(:,5),'g');
plot(hamp(:,7),'k');
plot(hamp(:,9),'m');
plot(hamp(:,11),'c');
plot(hamp(:,13),'y');
plot(hamp(:,15),'b');
plot(hamp(:,17),'r-');
legend('#1','#3','#5','#7','#9','#11','#13','#15','#17');