User Tools

Site Tools


octave:heartrate

Heart-rate detection

For best viewing experience watch the video full screen and in HD.

ECG files

This algorithm was used for detecting the heart rate while smoking a cigarette.

% uncomment for octave under windows
pkg load signal

load 'ii.dat';
y=ii(1:20000,2);
plot(y);

h = fir1(1000,1/1000*2,'high');

% filter out DC
y_filt=filter(h,1,y);
plot(y_filt);

% square it
detsq = y_filt .^ 2;

% thresholded output
detthres = zeros(length(detsq),1);

% let's detect the momentary heart rate in beats per min
last=0;
upflag=0;
pulse=zeros(length(detsq),1);
p=0;

for i = 1:length(detsq)
    if (detsq(i) > 0.1)
        if (upflag == 0)
            if (last > 0)
                t = i - last;
                p = 1000/t*60;
            end
            last = i;
        end
        upflag = 100;
    else
        if (upflag>0)
            upflag = upflag - 1;
        end
    end
    pulse(i)=p;
end

% plot it
plot (pulse);
octave/heartrate.txt · Last modified: 2014/01/26 13:57 by berndporr