title: Median filter at point of local maximas in MATLAB
body: <p>Is it possible for me to apply median filter only at points of local maxima’s. </p>
  
  <p>Like in my simple example local maxima is 5,7,8,4.</p>
  
  <p>Can I apply median filter at these points. I have also provided code with sin signal to understand the effect of median filter better. </p>
  
  <p>Can I apply median filter at point of local maxima’s.</p>
  
  <p>Thanks in advance.</p>
  
  <p><strong>Code :</strong></p>
  
  <pre><code>  A = [1 0 1 4.3 4.5 5 4.3 4.2 0 0 0 2 6.2 6.3 7 6.2 7.4 8  7.2 1 2 3 4 2];
    X= A;
  
    % For sin signal
    t= 0:0.01:10;
    A = sin(2*pi*t);
    X=A;
    t = 1:numel(X) ;
  
    [p l] = findpeaks(X);
  
    %  [pn ln] = findpeaks(-X);
    % yMedFilt = medfilt1(X,5,'truncate');
  
    y = medfilt1(X,2,'truncate');
    hold on 
    plot(p,'r','linewidth',3)
    plot(A,'b')
    hold off
  </code></pre>
  
  <p>I want is to have a straight line at every peak. Please have a look at attached picture . In the picture I am expecting output like 3rd diagram with straight line at every peak.</p>
  
  <p><a href="https://i.stack.imgur.com/UJxc0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/UJxc0.png" alt="enter image description here"></a></p>