close all;
clear all;


ft = 200;

t_1 = 0 : 1/ft : (4-(1/ft)); %czas od 1 do 4 co ft próbkowane
t_2 = 4 : 1/ft : (8-(1/ft));
t_3 = 8 : 1/ft : (12-(1/ft));
t_4 = 12 : 1/ft : 20;

s_1 = sin(9*t_1*2*pi + 0);
s_2 = square(10*t_2*2*pi + pi/4); % pi = 180Â°. 
s_3 = 1*randn(1,length(t_3));
s_4 = sawtooth(t_4,0.5);


t = [t_1, t_2, t_3, t_4];
s = [s_1, s_2, s_3, s_4];


hold on;
grid on;
figure(1);
plot(t,s, 'r');
%_____________________________________________________
[xw, fpw] = audioread('mbi04jeden.wav');
Nx=length(xw);
t_max =(Nx-1)/fpw;
txw = 0 :1/fpw:t_max;
figure(2);
plot (txw,xw,'c');
%sound(xw,fpw);
hold on;
grid on;
%___________________________ decymacja
xv = decimate(xw, 4);
fpv = fpw / 4;
tv = 0 : 1/fpv : (length(xv)-1)/fpv;
%sound(xv,fpv);
plot(tv,xv);
title('nagranie');
xlabel('czas [s]');
%_________________________ transformata po decymacji
Nfv=2^nextpow2(length(xv));
Nfv21 =Nfv/2+1;
frv = fft(xv,Nfv)

fv=linspace(0, fpv, Nfv);
wv=abs(frv);
figure(3);
hold on;
grid on;
%plot(fx(1:Nfx21),vx(1:Nfx21),'g');
plot(fv(1:Nfv21),wv(1:Nfv21));

%___________ filtracja
fgr=1000;
fgrN=fgr/(fpv/2);
M=3001;
h=fir1(M-1, fgrN,'high');
yv = filter(h,1,xv);
fryv = fft(yv,Nfv);
wyv=abs(fryv);
plot(fv(1:Nfv21),wyv(1:Nfv21));

%______________________korelacja

kmax = 400; %tk_max*fpw;
r = xcorr (s,s,kmax);
tr = (-kmax/ft : 1/ft : kmax/ft);

figure(6);
plot(tr, r,'r');
hold on;
grid on;
rr = xcorr(s,s_1,kmax);
plot(tr, rr,'g');
%___________________________ hist
figure(7);
hold on;
grid on;
hist(s_1,15);

