frequency mesurement of a square wave

hi I want to to measure the frequency of a square wave with simulink , do anybody know how to do this? thanks

 Accepted Answer

Image Analyst
Image Analyst on 22 Feb 2015
Edited: Image Analyst on 22 Feb 2015
I don't have Simulink but I think you can use a MATLAB block in it where you run MATLAB code. If so, then just threshold the signal and use find()
signal = [0 0 6 6 6 0 0 6 6 6 0 0 6 6 6 0 0]
halfMaxValue = 0.5 * (min(signal) + max(signal)) % Halfway between 0 and 6.
highSignals = signal > halfMaxValue
diffSignal = [0 diff(highSignals)]
diffSignalIndexes = find(diffSignal == 1)
period = diffSignalIndexes(2) - diffSignalIndexes(1) % Distance between first two pulses.
In the command window:
halfMaxValue =
3
highSignals =
0 0 1 1 1 0 0 1 1 1 0 0 1 1 1 0 0
diffSignal =
0 0 1 0 0 -1 0 1 0 0 -1 0 1 0 0 -1 0
diffSignalIndexes =
3 8 13
period =
5

9 Comments

I don't understand your code properly, can you explain it with an example please?
I've edited it to show an example where the pulse repeats every fifth element.
it's true, but it doesn't work with the signal data that goes from simulink to workspace, for examle this signal:(in 5 sec simulation)
signal=[1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1];
it's frequency is equal to 10, but this code gives it equal to 2 !!!!
hamid, my code doesn't give the frequency. It gives the period in units of elements. You can easily convert to find the frequency. Frequency is one over the period:
signal=[1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1;0;1]
timePerElement = 5 / (length(signal)-1) % from element 1 to element 101 (100 elements) = 5 seconds.
% My code found 2 elements is the period.
timePerPeriod = 2 * timePerElement;
frequency = 1 / timePerPeriod
hamid
hamid on 23 Feb 2015
Edited: hamid on 23 Feb 2015
OK, that's right.Now another question: if the frequency of signal changes with time,for measuring the maximum frequency ,what is your suggestion? thanks
Look at diffSignalIndexes - those are the leading edges. THe highest frequency will be where the distance between indexes is least. So you want to call min(diff(diffSignalIndexes)) to find the min distance. Then use the frequency formula to give you the max frequency.
I made this signal with simulink and put it's minimum period equal to 0.2,so maximum frequency will be 5 as you see. then take the signal data to script and write your code with this form.but it does'nt give the right answer, it gives 1.63 !!
can you help me to debug this code please? thanks
signal=[1;1;1;1;1;0;0;0;0;1;1;1;1;1;0;0;0;0;0;1;1;1;1;1;0;0;0;0;0;1;1;1;1;1;0;0;0;0;0;0;0;1;1;1;0;0;0;1;1;1;1;1;1;1;1;0;0;0;0;1;1;1;1;1;0;0;0;0;0;1;1;1;1;1;1;0;0;0;0;1;1;1;1;1;0;0;0;0;0;1;1;1;1;1;0;0;0;0;0];
halfMaxValue = 0.5 * (min(signal) + max(signal))
highSignals = signal > halfMaxValue
diffSignal = diff(highSignals)
diffSignalIndexes = find(diffSignal == 1)
m=min(diff(diffSignalIndexes))
timePerElement = 10 / (length(signal)-1) %10 sec is simulation time
timePerPeriod = m* timePerElement
max = 1 / timePerPeriod
Your signal doesn't quite look right. The plot seems to show it going high around element 43 or 44 but your signal variable shows it going up around element 42.
bravo ! my simulink solver was incorrect, I corrected it and got the right answer 5 Hz. very very very thanks <3

Sign in to comment.

More Answers (1)

MoeIsKing
MoeIsKing on 24 Jun 2018
Edited: Image Analyst on 24 Jun 2018
If i run:
highSignals = signal > halfMaxValue
over my signal i get:
"Error using horzcat
Dimensions of arrays being concatenated are not consistent."
What did i wrong? My "highSignals" type is "2002x1 logical" after I run:
highSignals = volts > halfMaxValue ;
halfMaxValue is 12 and volts comes from a scope (light bar) and is imported as csv into "2002x1 double"

1 Comment

It looks OK to me. What I don't understand is how you could execute the line to create highSignals successfully as a 2002 by 1 vector, and yet still have an error thrown? In my experience if an error is thrown, you will not get the result put into the output variable. Can you explain that? Exactly what line of code are you executing when the error is thrown?

Sign in to comment.

Categories

Find more on Simulink in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!