Effacer les filtres
Effacer les filtres

Find Middle of square wave

7 vues (au cours des 30 derniers jours)
Russell Senior
Russell Senior le 2 Avr 2020
Commenté : Star Strider le 2 Avr 2020
I can find the rising edge and falling edge of a square wave, any thoughts on how to find the middle of an arbitrary square wave? I created a simple code using a logical array that finds the rising edges and falling edges, but I can't think of a good way to find the middle of the square. Ideas?
Edit: I'd like to avoid using a for loop. Speed is important since this will be used to process a few GB of data.
clear all
close all
t = 0:100;
x = false(size(t));
x(2:10) = true;
x(25:30) = true;
x(50:75) = true;
x(90:end) = true;
rising = x > [x(1) x(1:end-1)];
falling = x < [x(1) x(1:end-1)];
subplot(3,1,1)
plot(t,x)
subplot(3,1,2)
plot(t,rising,'r')
subplot(3,1,3)
plot(t,falling,'k')

Réponse acceptée

Star Strider
Star Strider le 2 Avr 2020
Use the islocalmax function (R2017b and later):
This code plots green upward-pointing triangles at the centre of each pulse:
t = 0:100;
x = false(size(t));
x(2:10) = true;
x(25:30) = true;
x(50:75) = true;
x(90:end) = true;
rising = x > [x(1) x(1:end-1)];
falling = x < [x(1) x(1:end-1)];
ctr = islocalmax(x, 'FlatSelection','center'); % Added
subplot(3,1,1)
plot(t,x)
hold on
plot(t(ctr), x(ctr), 'g^') % Added
hold off
subplot(3,1,2)
plot(t,rising,'r')
subplot(3,1,3)
plot(t,falling,'k')
I did not add them to the last two subplot axes. Copy the hold and plot calls to them if you want to plot them there as well.
  2 commentaires
Russell Senior
Russell Senior le 2 Avr 2020
That is exactly what I was looking for. Thanks for that.
Star Strider
Star Strider le 2 Avr 2020
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by