find locations at which oscillations cross axis using the findpeaks() function in matlab

You can find locations at which oscillations cross the axis (zero crossings) using the findpeaks() function in Matlab by setting its MinPeakHeight property to 0.

Here is an example code that demonstrates how to use findpeaks() function to find zero crossings:

main.m
% Create a simple sine wave signal
t = 0:0.01:2*pi;
y = sin(t);

% Find peaks and locations where signal crosses zero
[~,locs] = findpeaks(y,'MinPeakHeight',0); 

% Plot the signal and show the locations of zero crossings
plot(t,y);
hold on;
plot(t(locs),y(locs),'ro');
hold off;
280 chars
13 lines

The findpeaks() function returns the amplitudes of the peaks as the first output argument. Since we are interested only in the locations of zero crossings, we can ignore the output by using the tilde (~) symbol. The second output argument contains the indexes of the zero crossings.

In the above code, we first define a simple sine wave signal. Then, using findpeaks() function with MinPeakHeight property set to zero, we find the locations of zero crossings. Finally, we plot the signal and show the locations of zero crossings as red dots.

related categories

gistlibby LogSnag