Easy way of finding zero crossing of a function

Hi guys, I have tried all my attempts but not succeeding to get the correct output/plot .
I'm trying to find my zero crossings points that are implicitly the intercestion of x axis, in other words I want to find all the zero crossings points of my signal by interpolation method!.
I've searched in the other threads over this forum and find subjects that are related to what Im asking but not exactly what I want or either their answers weren't totally correct!
I have signal called y1, it's vectors of samples, I sampled it on sample frequency 2048KHz .
accordingly to @ Star Strider code:
t = [1:length(y1)]; % Time Vector
y = y1; % Signal
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Zero-Crossing Indices Of Argument Vector
zx = zci(y); % Approximate Zero-Crossing Indices
figure(1)
plot(t, y, '-r')
hold on
plot(t(zx), y(zx), 'bp')
hold off
grid
legend('Signal', 'Approximate Zero-Crossings')
when I do plot , Im not getting zero crossings points that are the intersections of x axis by interpolation method! , here's what I get:
what Im willing to do by method interpolation to find the zero crossings point which gives me like this result:
could anyone please help me how can I find the zero crossings points by interpolation to get like the above plot (zero crossings points like they are the intersections with x axis) ?
the code of my interpolation for finding the zero crossings that Im trying is: (Fs=2048Khz) - thanks to
if size(y1,1) == 1; y1 = y1.'; end %ensure columns of signal
c = size(y1,2);
wl = (0:size(y1,1)-1) / Fs;
Iwl2=360:0.001:740; % interpolating to 0.001-nm resolution
loc_frequ1=0;
for i=1:c
y = y1(:,i);
y2=interp1(wl,y,Iwl2); % interpolating to 0.001-nm resolution
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Zero-Crossing Indices Of Argument Vector
zx = zci(y2); % Approximate Zero-Crossing Indices
loczeros1=round(Iwl2(zx));
locfrequ= ismember(Iwl2,loczeros1); % find same data from Iwl2
loc_frequ1=loc_frequ1+double(locfrequ);
end
>> c
c =
1
>> zx
zx =
0×1 empty double column vector
>> size(y1) %size of my signal data
ans =
9001 1
but the problem with this that this code didn't work and don't know what the problem is, zx isn't giving me the zero crossings point at all ..
Could anyone please help me how can I do and find the zero crossings points by interpolation in matlab? I really searched about other threads that are related to this but the answers weren't enough or didn't work for my case.

4 comentarios

dpb
dpb el 16 de Jul. de 2020
You didn't take the portion of the code that did the interpolation but just picked the initial part that located the sampled points at the zero crossings.
The rest of the code to do the interpolation follows -- looking through the catalog of Star's many responses, the one cleanest overall posting appears to be <Answers/295961-using-interpolation-to-find-the-zeros-of-a-data-set-that-forms-a-sinusoidal-profile>
Again, you need THE WHOLE FUNCTION, not just the initial crossings location logic.
I would also recommend that you make the change from circshift to the find(diff() form that he recommends in the first link.
Mohamed Jamal
Mohamed Jamal el 16 de Jul. de 2020
Editada: Mohamed Jamal el 16 de Jul. de 2020
thanks, so according to what you attached, x0 has the zero crossings points right? it's a vector of zero crossings points ..
here's the code that Im referring to:
y = y1;
x = 1:length(y1); % Create Data
zci = @(v) find(v(:).*circshift(v(:), [-1 0]) <= 0); % Returns Approximate Zero-Crossing Indices Of Argument Vector
dy = zci(y); % Indices of Approximate Zero-Crossings
for k1 = 1:size(dy,1)-1
b = [[1;1] [x(dy(k1)); x(dy(k1)+1)]]\[y(dy(k1)); y(dy(k1)+1)]; % Linear Fit Near Zero-Crossings
x0(k1) = -b(1)/b(2); % Interpolate ‘Exact’ Zero Crossing
mb(:,k1) = b; % Store Parameter Estimates (Optional)
end
freq = 1./(2*diff(x0)); % Calculate Frequencies By Cycle
figure(1)
plot(x, y)
hold on
plot(x0, zeros(size(x0)), '+r')
hold off
grid
Mohamed Jamal
Mohamed Jamal el 16 de Jul. de 2020
thanks alot for your help guys, all fine succeeded !
thanks to you dpb
dpb
dpb el 16 de Jul. de 2020
Good to hear...

Iniciar sesión para comentar.

Respuestas (0)

Etiquetas

Preguntada:

el 16 de Jul. de 2020

Comentada:

dpb
el 16 de Jul. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by