How count pulse with matlab?
8 views (last 30 days)
Show older comments
I have the signal outputs from the rotary encoder. I'd like to count pulses using Matlab software. How can I count pulses? I attach the signal outputs. Would you please find the attachment files. Any codes? Any Solutions?
3 Comments
Walter Roberson
on 7 Apr 2016
Is your question http://www.mathworks.com/matlabcentral/answers/266200-counting-pulses-of-rotary-encoder complete?
Answers (1)
Image Analyst
on 7 Apr 2016
Find the bottom envelope by using imerode() (in the Image Processing Toolbox). Then subtract the envelope from the signal. Then threshold and call bwlabel to count the pulses. Or you can use diff but it's more complicated.
bottomEnvelope = imerode(signal, true(1, windowWidth));
flattenedSignal = signal - bottomEnvelope;
hold on;
plot(flattenedSignal, 'r-');
binarySignal = flattenedSignal > 50;
plot(binarySignal, 'r-');
[~, numberOfPulses] = bwlabel(binarySignal);
18 Comments
SHANTANU KSHIRSAGAR
on 31 May 2020
the findchangepts is not working due to absence of signal processing toolox. Is there any way similarly robust.
Image Analyst
on 31 May 2020
Find the first place where the signal exceeds 2.5 and then fall down the left side until it turns around.
% Initialization steps. Brute force cleanup of everything currently existing to start with a clean slate.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
s = load('req4.mat')
x = s.x1;
y = s.z1;
subplot(2, 1, 1);
plot(x, y, 'b-', 'LineWidth', 2);
index = find(y > 2.5, 1, 'first');
hold on;
plot(x(index), y(index), 'r*', 'LineWidth', 2, 'MarkerSize', 15);
grid on;
% This finds a point on the upward slope. Fall down the slope until it starts to turn around again.
while y(index) > y(index - 1) && index >= 2
index = index - 1;
end
xline(x(index), 'Color', 'r', 'LineWidth', 2);
caption = sprintf('Original Signal with %d elements. Start crop at index = %d, x = %f', length(x), index, x(index));
fontSize = 20;
title(caption, 'FontSize', fontSize);
xlabel('x1', 'FontSize', fontSize);
ylabel('z1', 'FontSize', fontSize);
% Extract just the part to the right of the index (red line).
x2 = x(index : end);
y2 = y(index : end);
subplot(2, 1, 2);
plot(x2, y2, 'b-', 'LineWidth', 2);
grid on;
xlim([min(x2), max(x2)]);
xlabel('x1', 'FontSize', fontSize);
ylabel('z1', 'FontSize', fontSize);
g = gcf;
g.WindowState = 'maximized';
caption = sprintf('Cropped Signal with %d elements.', length(x2));
title(caption, 'FontSize', fontSize);
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/307115/image.png)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!