title: QRS detection(peaks) of a raw ecg signal in matlab
body: <p>I want to find the peaks of the raw ecg signal so that I can calculate the beats per minute(bpm).
  I Have written a code in matlab which I have attached below.In the code below I am unable to find threshold point correctly which will help me in finding the peaks and hence the bpm.</p>
  
  <pre><code>%input the signal into matlab
  
  [x,fs]=wavread('heartbeat.wav');
  subplot(2,1,1)
  plot(x(1:10000),'r-')
  grid on
  
  
  
  %lowpass filter the input signal with cutoff at 100hz
  h=fir1(30,0.3126);     %normalized cutoff freq=0.3126
  y=filter(h,1,x);
  
  subplot(2,1,2)
  plot(y(1:10000),'b-')
  grid on
  
  
  
  % peaks are seen as pulses(heart beats)
  beat_count=0;
  for p=2:length(y)-1
      th(p)=abs(max(y(p)));
      if(y(p) &gt;y(p-1) &amp;&amp; y(p) &gt;y(p+1) &amp;&amp; y(p)&gt;th(p))
          beat_count=beat_count+1;
      end
  end
  
  N = length(y);
  duration_seconds=N/fs;
  duration_minutes=duration_seconds/60;
  BPM=beat_count/duration_minutes;
  bpm=ceil(BPM);
  </code></pre>
  
  <p>Please help me as I am new to matlab</p>