function siganalysis(x, fs)
% signanalysis(x, fs)
% Plot the waveform of signal x and the spectrogram
% fs - sampling frequency

N = length(x);
T = N/fs; % duration in seconds
frame = fix(.05*fs); % 50 ms frame
for m=1:fix(N/frame)
	rms(m) = sqrt(sum(x(1+(m-1)*frame:m*frame).^2)/frame);
end
theta_rms = .0;
offset = frame*(min(find(rms>theta_rms)))+1;
xend = min(frame*(max(find(rms>theta_rms)))+5*frame,N);
%
M = 5; % Number of waveform plots/figure
Tframe = .2; % duration of each plot in seconds
L = ceil((xend-offset)/(M*Tframe*fs)); % number of figures
for l=1:L
	figure(l+1); clf
	for m=1:M
		i1=offset+fix(((l-1)*M*Tframe+(m-1)*Tframe)*fs);
		i2 = i1 + fix(Tframe*fs);
		if i2 > xend
			m=m-1;
			drawnow
			break
		end
		subplot(M,1,m);
		time = ([i1:i2]-offset)/fs;
		plot(time, x(i1:i2));
		axis([time(1) time(1)+Tframe -1 1]);
	end
	if m > 0
		subplot(M,1,1); title(sprintf('Waveform Plot %d',l));
		subplot(M,1,fix((m+1)/2)); ylabel('Signal Amplitude');
		subplot(M,1,m); xlabel('Time (seconds)');
		drawnow
	else
		clf
	end
end
%
figure(1)
specgram(x(offset:xend),256,fs);
title('Spectrogram')
