% $Id$

function[] = sigpow()

% sample_rate = 44100;
sample_rate = 8000
window_size = 2048;

[recDevId, recDev] = select_rec_device;
if playrec('isInitialised')
	playrec('reset');
end
playrec('init', sample_rate, -1, recDevId)

b1 = 600.0/(sample_rate / 2) / 1.01
b2 = 600.0/(sample_rate / 2) * 1.01

b = fir1(200, [b1 b2], "pass");
freqz(b,1,512,"half",sample_rate)

fftSize = window_size * 2;
fftFigure = figure;
fftAxes = axes('parent', fftFigure, 'xlimmode', 'manual', ...
	'ylimmode', 'manual', 'xscale', 'linear', 'yscale', 'linear', ...
	'xlim', [0, 2000], 'ylim', [-30, 70]);
fftLine = line('XData', (0:(fftSize/2))*sample_rate/fftSize, ...
	'YData', ones(1, fftSize/2 + 1));

playrec('delPage');

while true
	page = playrec('rec', 20 * sample_rate, 1);
	playrec('block', page);
	[recBuffer, recChanList] = playrec('getRec', page);
	y = filter(b, 1, recBuffer);
	% y = recBuffer;
	recFFT = fft(y(1:fftSize));
	set(fftLine, 'YData', 20*log10(abs(recFFT(1:fftSize/2 + 1))));
	drawnow;
	sum = y' * y / length(y);
	fprintf("pow = %.2f dB\n", log(sum)/log(10)*10);
	fflush(stdout);
end

