MATLAB: How to remove negative peaks from a signal

MATLABnegative peaks removal

How to remove negative peaks from a signal

Best Answer

sandhya, it looks like you didn't even read the comments above since there is no plot of what you are starting with, no plot of what you'd want as an output, and no explanation of what defines a negative peak (valley).
I wrote a program to smooth the data and remove valleys so that the final signal is just lines between the peaks of the noise-reduced signal.
% Custom program for sandhya to remove valleys from a signal.
clc; % Clear the command window.
clearvars;
close all; % Close all figures (except those of imtool.)
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
s = load('s4.mat')
val = s.val
subplot(2, 1, 1);
plot(val, 'b-', 'LineWidth', 2);
grid on;
xlabel('index', 'FontSize', fontSize);
ylabel('val', 'FontSize', fontSize);
caption = sprintf('val vs. index for %d elements', length(val));
title(caption, 'FontSize', fontSize);
% Signal is pretty noisy. Smmoth it with a Savitzky-Golay filter (moving quadratic).
filterWindowWidth = 91;
smoothedSignal = sgolayfilt(val, 2, filterWindowWidth);
subplot(2, 1, 2);
plot(smoothedSignal, 'b-', 'LineWidth', 2);
grid on;
xlabel('index', 'FontSize', fontSize);
ylabel('Smoothed Signal', 'FontSize', fontSize);
caption = sprintf('Smoothed Signal index for %d elements', length(val));
title(caption, 'FontSize', fontSize);
%------------------------------------------------------------------------------

% Set up figure properties:

% Enlarge figure to full screen.

set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.

% set(gcf, 'Toolbar', 'none', 'Menu', 'none');

% Give a name to the title bar.

set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% Find peaks in the smoothed signal - requires Signal processing Toolbox.
[peakValues, peakIndexes] = findpeaks(smoothedSignal,'MinPeakProminence', 3, 'Annotate', 'extents'); % Use default parameters
% Tack on first and last points.
peakIndexes = [1, peakIndexes, length(val)];
peakValues = [1, peakValues, val(end)];
% Plot results.
figure;
subplot(2, 1, 1);
plot(smoothedSignal, 'b-', 'LineWidth', 2);
hold on;
plot(peakIndexes, peakValues, 'rv', 'LineWidth', 2, 'MarkerSize', 10);
plot(peakIndexes, peakValues, 'r-', 'LineWidth', 1);
grid on;
xlabel('index', 'FontSize', fontSize);
ylabel('val', 'FontSize', fontSize);
caption = sprintf('Location of %d peaks', length(peakIndexes));
title(caption, 'FontSize', fontSize);
% Interpolate between peaks
allIndexes = 1 : length(val);
yInterp = interp1(peakIndexes, val(peakIndexes), allIndexes);
subplot(2, 1, 2);
plot(val, 'b-', 'LineWidth', 1);
hold on;
plot(allIndexes, yInterp, 'r-', 'LineWidth', 2);
grid on;
xlabel('index', 'FontSize', fontSize);
ylabel('fitted vals', 'FontSize', fontSize);
title('Final Signal in red with negative peaks (valleys) removed', 'FontSize', fontSize);
legend('Original Signal', 'Final, valleys-removed signal');
%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
I hope it's what you want. Since you didn't provide specifics, I guess you should be happy with anything that we do. I think I put more time into your problem than you did, so I hope my time was not wasted.
If it's not what you want, then re-read the comments above (under your original question at the top) and fix/improve your question.