% On-line recording of signal : Microphone -> MATLAB

% Older version using WAVRECORD
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% fs=16000;  % 8000, 11025, 22050, 44100, 48000, and 96000 Hz
% channels=1;  % 1 or 2 (mono or stereo)
% 
% to = 5 ;   % signal duration in seconds
% 
% 
% sig = wavrecord ( to*fs, fs, channels ) ;

% New version using AUDIRECORDER
%   MATLAB 2014 and higher
%%%%%%%%%%%%%%%%%%%%%%%%%%%

fs=16000;  % 8000, 11025, 22050, 44100, 48000, and 96000 Hz
bits=16;   % 8, 16, or 24
channels=1;  % 1 or 2 (mono or stereo)

to = 5 ;   % signal duration in seconds

r = audiorecorder(fs, bits, channels);

record(r);     % speak into microphone...
pause(to) ;
stop(r);

sig = getaudiodata(r, 'double'); % get data as int16 array and norm them to the interval <-1, 1>
 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Possible observing of the result
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

figure(1)
subplot(211)
plot((1:length(sig))./fs,sig)
subplot(212)
wlen=floor(0.032*fs);
spectrogram(sig,wlen,[],[],fs,'yaxis')
