Skip to content

Instantly share code, notes, and snippets.

@jjcarrier
Created February 19, 2012 18:18
Show Gist options
  • Save jjcarrier/1864955 to your computer and use it in GitHub Desktop.
Save jjcarrier/1864955 to your computer and use it in GitHub Desktop.
Zero-Crossings example
clear all;
close all;
clc;
tArray=0:.01:4;
%Add some noise for good measure
noise=.1.*randn(1,length(tArray));
vArray=sin(tArray).*sin(3*pi.*tArray)+noise;
%computes the difference of each neighboring
%element in the boolean expression
test=diff(vArray>=0);
%Note: test is one element shorter than vArray
ind=find(test); %Grabs index values where
%test is non-zero
%Compute the linear interpolation
zeroCrossings=[];
for x=1:length(ind)
rise=vArray(ind(x))-vArray(ind(x)+1);
run=tArray(ind(x))-tArray(ind(x)+1);
m=rise/run;
b=vArray(ind(x))-m*tArray(ind(x));
zeroCrossings=[zeroCrossings,-b/m];
end
%Plot the results
plot(tArray,vArray,'Marker','.');
hold on
plot(zeroCrossings,zeros(1,length(ind)),'rs')
grid on;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment