Measuring Peak-to-Peak

Jeff T September 30, 2011 Coded in Matlab

Sometimes you just need a simple Matlab function that does a task that its function name describes exactly.  In this case, I wanted a simple peak-to-peak function that does the same thing the my oscilloscope does.  Of course, the code is fairly simple but it is always nice to have around so you don't have to code this repeatably in each m-file application.

function y = peak2peak(signal)
% Return the peak-to-peak amplitude of the supplied signal.  This is the
% same as max(signal) minus min(signal).
% Usage: y = PEAK2PEAK(signal);
%        SIGNAL is your one-dimensional input array
% Author: sparafucile17

% Input must have some length
if(length(signal) == 1)
    error('ERROR: input signal must have more than one element');

% This function only supports one-dimensional arrays
if((size(signal, 2) ~= 1) && (size(signal, 1) ~= 1))
    error('ERROR: Input must be one-dimensional');

% Find the peak and return it
min_sig = min(signal);
max_sig = max(signal);

% Answer
y = max_sig - min_sig;