How to remove negative peaks from a signal

20 views (last 30 days)
How to remove negative peaks from a signal
  3 Comments
sandhya sandhya
sandhya sandhya on 8 Sep 2018
I want to detrend the signal first and then i want to remove all the negative peaks from the signal.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 8 Sep 2018
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.
  10 Comments
sandhya sandhya
sandhya sandhya on 17 Sep 2018
1.I want to set all those negative peaks to zero and its an optional.I want to work only on positive peaks.After that i want to calculate average amplitude of the signal.
2.I applied detrending technique based on the command detrend().If polyfit() command suits to my signal then apply that command to my signal.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!