freqM=44000;
s_length=0.01;
lasttoshow=256;
pauseM=0.1;
timeM=linspace(0,lasttoshow*1/freqM,lasttoshow);

r = audiorecorder(freqM, 24, 2);
recordblocking(r,s_length);
clear dataM;
dataM = getaudiodata(r);

subplot(4,2,1:4);
plothandle_phase=plot(dataM(1:100,1),dataM(1:100,2));
xlim([-1 1]);
% ylim([-0.2 0.2]);
subplot(4,2,5);
plot_x=plot(timeM,dataM(1:lasttoshow,1));
subplot(4,2,6);
plot_y=plot(timeM,dataM(1:lasttoshow,2));

Y(1,:)=fft(dataM(:,1)',1024);
Y(2,:)=fft(dataM(:,1)',1024);
Pyy(1,:)=Y(1,:).*conj(Y(1,:))/1024;
Pyy(2,:)=Y(2,:).*conj(Y(2,:))/1024;
freks=1000*(0:512)/1024;

subplot(4,2,7);
plot_frek_x=plot(freks,Pyy(1,1:513));
subplot(4,2,8);
plot_frek_y=plot(freks,Pyy(2,1:513));

while(1)
    recordblocking(r,s_length);
    dataM = getaudiodata(r);
    
    set(plothandle_phase,'XData',dataM(1:lasttoshow,1));
    set(plothandle_phase,'YData',dataM(1:lasttoshow,2));    
    
    set(plot_x,'XData',timeM);        
    set(plot_x,'YData',dataM(1:lasttoshow,1));    

    set(plot_y,'XData',timeM);        
    set(plot_y,'YData',dataM(1:lasttoshow,2));    

    Y(1,:)=fft(dataM(:,1)',1024);
    Y(2,:)=fft(dataM(:,1)',1024);
    Pyy(1,:)=Y(1,:).*conj(Y(1,:))/1024;
    Pyy(2,:)=Y(2,:).*conj(Y(2,:))/1024;    
    
    set(plot_frek_x,'XData',freks);        
    set(plot_frek_x,'YData',Pyy(1,1:513));    

    set(plot_frek_y,'XData',freks);        
    set(plot_frek_y,'YData',Pyy(2,1:513));    
    
    pause(pauseM);
end

stop(r);