title: How to generate smooth filtered envelope on EMG data in Matlab
body: <p>I'm new to analysing EMG data and would appreciate some carefully explained help.</p>
  
  <p>I would like to generate a smooth, linear enevelope signal of my EMG data (50kHz sampling rate) like the one published in this paper: <a href="https://openi.nlm.nih.gov/detailedresult.php?img=PMC3480942_1743-0003-9-29-3&amp;req=4" rel="nofollow noreferrer">https://openi.nlm.nih.gov/detailedresult.php?img=PMC3480942_1743-0003-9-29-3&amp;req=4</a></p>
  
  <p>My end goal is to be able to analyze the relationship between EMG activity (output) and action potentials fired from upstream neurons (putative input)  recorded at the same time.</p>
  
  <p>Even though this paper lists the filtering methods out quite clearly,  I do not understand what they mean or how to perform them in matlab, which is the analysis tool I have available to me. </p>
  
  <p>In the code I have written so far, I can dc offset as well as rectify my data:</p>
  
  <pre><code>x = EMGtime_data
  y = EMGvoltage_data
  
  %dc offset
  y2=detrend(y)
  % Rectification of the EMG signal
  rec_y=abs(y2);
  plot(x, rec_y)
  </code></pre>
  
  <p>But then I am not sure how to proceed.
  I have tried the envelope function, but it is not as smooth as I would like:
  For instance, if I used the following:</p>
  
  <pre><code> envelope(y_rec,2000,'rms')
  </code></pre>
  
  <p>I get this (which also doesn't seem to care that the data is rectified):</p>
  
  <p><a href="https://i.stack.imgur.com/DwqLq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DwqLq.png" alt="enter image description here"></a></p>
  
  <p>Even if I were to accept the envelope function, I'm not sure how to access just the processed envelope data to adjust the plot (i.e. change the y-range), or analyse the data further for on-set and off-set of the signal since the results of this function seem to be coupled with the original trace.</p>
  
  <p>I have also come across fastrms.m, which seems promising. Unfortunately, I do not understand how to implement this function since the general explanation is over my head and the example code is lacking any defined variable (so I don't know where to integrate my own data!)
  The example code from fastrms.m file exchange is here</p>
  
  <pre><code>Fs = 200; T = 5; N = T*Fs; t = linspace(0,T,N); 
  noise = randn(N,1); 
  [a,b] = butter(5, [9 12]/(Fs/2)); 
  x = filtfilt(a,b,noise); 
  window = gausswin(0.25*Fs); 
  rms = fastrms(x,window,[],1); 
  plot(t,x,t,rms*[1 -1],'LineWidth',2); 
  xlabel('Time (sec)'); ylabel('Signal') 
  title('Instantaneous amplitude via RMS')
  </code></pre>
  
  <p>I will be eternally grateful for help in understanding how to filter and smooth EMG data!</p>