title: Resampling a EEG Signal with non-constant sampling frequency
body: <p>I have a EEG Signal with a non-constant sampling frequency as shown in the image below.</p>
  
  <p><img src="https://i.stack.imgur.com/88VlU.jpg" alt="EEG Signal with a non-constant sampling frequency"></p>
  
  <p>I'm trying to do resampling on the EEG Signal to achieve a constant sampling frequency so that it can be better used for further analysis.</p>
  
  <p><img src="https://i.stack.imgur.com/6JzWz.jpg" alt="EEG Signal with a constant sampling frequency"></p>
  
  <p>Right now, I'm breaking the signal into time frames of 1 second each, and resampling them using the <a href="http://www.mathworks.com/help/signal/ref/resample.html" rel="nofollow noreferrer">resample</a> function to get a constant sampling frequency. However, the resulting EEG Signal seems to be incorrect, with weird fluctuations at the connecting points.</p>
  
  <p><img src="https://i.stack.imgur.com/vUymX.jpg" alt="Resampled EEG Signal"></p>
  
  <p>I don't think that I'm doing this correctly, so if it is possible, can someone provide a better suggestion or point me in the right direction? Thanks.</p>
  
  <p><strong>Existing code (executed in a for loop that goes through the entire EEG signal in time frames of 1 second each):</strong></p>
  
  <pre><code>% Calculate resampling factors p &amp; q
  % Fs(currSecond) gives the number of samples within that time frame of 1 second
  % nFS is the target sampling frequency (constant) to be achieved for each time frame
  
  [p, q] = rat(nFs/Fs(currSecond), 0.0000000001);
  
  % Calculate the end of data to be resampled
  endSample = currSample + Fs(currSecond) - 1;
  
  % Resample EEG data
  EEG_Data_Resampled = ...
      resample(EEG_Data_Trimmed(currSample : endSample, 1:4), p, q, 0);
  
  % Store resampled EEG data
  nCombinedData(startRow : endRow, 2:5) = num2cell(EEG_Data_Resampled);
  
  % Update starting sample for next iteration
  currSample = currSample + Fs(currSecond);
  </code></pre>
  
  <p><br>
  <strong>Edit:</strong> Changed the <code>resample</code> function to use a n value of 0</p>
  
  <pre><code>% If you let n = 0, resample performs a nearest-neighbor interpolation
  % Resample EEG data
      EEG_Data_Resampled = ...
          resample(EEG_Data_Trimmed(currSample : endSample, 1:4), p, q, 0);
  </code></pre>
  
  <p>And it seems to give me better resampled data, but I'm still not sure if the features of the original EEG Signal is being preserved for the resampled version.</p>
  
  <p><img src="https://i.stack.imgur.com/OWP7V.jpg" alt="enter image description here"></p>