I attached the signal output of the encoder. I want to count the number of pulses. How can I count pulses using Matlab software. I attached the signal outputs. Would you please help me.
Hi, differentiate the signal using the diff command. The derivative is a high pass filter which will remove the sine signale (low freq) and will amplify the rapid variations. You should get a sequence of peaks (negative and positive). Try to count them using the function findpeaks
a 'sizzling hot' tip: seems you have a bad or missing ground connection (the sine which is overlayed by the pulse train). First try to get rid of the sine - this will make pulse counting much easier.
Looked another time at the images: sine with variable frequency ? Or do you have different sampling rates. If variable frequency: motor inverter ?
below you can find the matlab code to solve your problem. I wrote the 1st part to reproduce in some way the signal you have. The second part is to recover the number of pulses. This last part is based on the concept I gave you as first answer. Hope that will help yuo.
close all;
clear all;
% 1st part used to generate the signal with pukses
t=8700:0.01:9100;
%
% sine wave
A=200;
f1=0.02;
x=A*cos(2*pi*f1*t);
%
% square wave
B=60;
f2=0.3;
y=B*square(2*pi*f2*t);
%
% NOISE
C=1;
r=C*rand(1,length(t));
%
% MIX them
z=x+y+r;
% 2nd part your code starts here replace z with your signal
% and B with the amplitude of the square wave e.g. 60
Yes, a differentiating filter will remove the interfering (low-freq) sinusoid, but it may also introduce a lot of noise. So you might get peaks due to high freq ripples that are not due to pulses from your encoder.
I think a better approach would be to make a near-dc notch filter. The natural roll-off (roll-on?) of this filter will also will also remove the interference, but the other higher-freq info should be unchanged.
The easiest way to design this dc notch filter would be just to compute the running average, over a finite (sliding) window of M samples (for M odd). This would be an FIR filter. You could realize this filter recursively, but you may get accumulated rounding errors after a (long) while. Subtract the computed mean from the sample at the centre of your sliding window, for an exactly linear-phase filter. This difference is the output of your filter. It should be easier to see and count the pulses in that filtered signal, using simple threshold-crossing logic.
The width of the dc notch increases, as M is decreased. As you can imagine, the accuracy of the mean computation increases as M increases, which makes the notch, or the dc-null, narrower.
Alternatively, you could compute a running exponential average recursively, using an exponentially decaying weight, for an IIR filter, with a recursive realization, without potential rounding error problems. The rate of exponential decay for this IIR replaces the M parameter for the FIR filter. That is, it defines the temporal selectivity and the width of the dc notch.
A lot of my recent research has been looking into extensions, analysis, and applications of these ideas: using higher-order polynomials, with different shapes of weighting functions.