Finding local minima
The following code snippet is something that I put together for a school project some time ago. In this project I was trying to apply some speech processing techniques to alter the pitch of a male speakers voice. At the time I was searching for the formants and trying to move them manually in the frequency domain.
I used this in conjunction with my local maxima code to find the position and the width (using local minima) of the speakers formants. If you give this function a 1-dimensional vector it will return both the indices in which each local minimum is found as well as it's amplitude.
I hope this code can find its way to other fun and exciting applications!
function [time,amp] = minima(signal, len)
%
% Finds the local minimum values of the given signal.
%
% Usage: [IND,AMP] = MINIMA(X, N);
%
% N is the number of points that need to be evaluated
% (Normally equal to LENGTH(X))
% X is the data
% IND contains the indices of X that contain the minima data
% AMP contains the minima amplitudes
%
% Author: sparafucile17 10/02/2003
%Initialize data
index = 1;
prev = signal(1);
local_time = 0;
local_amp = 0;
prev_slope = 1; %allow a maxima point at the second sample
%prevent length error
if(len > length(signal))
len = length(signal)
end
%Loop through data and find local minimums
for loop_i=2:len
cur = signal(loop_i);
slope = cur - prev;
if((cur < prev) & (prev_slope > 0)) %Positive slope and amplitude dips
local_time(index) = loop_i-1;
local_amp(index) = prev;
index = index+1;
end
prev = cur;
prev_slope = slope;
end
%After loop assign data to output variables
time = local_time;
amp = local_amp;