# Thread: Algorithm to identify the correct weight of a piece.

1. ## Algorithm to identify the correct weight of a piece.

I am studying the best way to identify the real weight of a product that passes over a belt.
The system receives about 20 weighings, from the moment the scale is empty until the piece leaves the belt.

I need to discard the weights I receive when the product is entering or leaving the platform, and pick up only the middle weights, which are closer to each other.

Basically I need to find out the approximate weight, but I have an acceptable margin of error (1g up or down)

I need to eliminate the weights that are distant .... out of the center of the curve in the graph.

In this case the weight is 76 grams.  2. I would use a state machine type code design.
Two states are "weight increasing" and "weight decreasing".
The main decision is should there be a third state of "weight peak" or related name.
I think NOT but, it really depends on the data sets.

Tim S. 3. > The system receives about 20 weighings, from the moment the scale is empty until the piece leaves the belt.
Answer 1 - you always pick the 10th sample.

Answer 2 - you only store samples if ( abs(s[n]-s[n+1]) < threshold ). You then average those. 4. It depends on the size of the part being weighed.
The weighing platform is 30cm, a piece of 80g is longer in the center (straight) of the weighing curve and takes less time to go up and down.

For a 200g piece, the rise / fall is longer and there are fewer samples in the center (straight) of the weighing curve.

The amount of weights can vary, but very little.
It receives 150 pesos per second, the time the piece passes through the platform for weighing is approximately 250ms.

Therefore, the same amount of weights will be received for any piece size, however the curve will vary according to the weight.

I discussed this topic in a Mathematics forum, but I could not understand how to convert from Wolfram to a programming code.

Http://mathhelpforum.com/statistics/...ght-piece.html 5. I can't see the picture detail without registering.
Also, all of this would have been useful information in your original post. 6. romsek's mathematica code from the mathhelpforum website (with the correction suggested by the author). romsek's description of his algorithm:

1) list the weights in time sequence

2) apply a slope detection filter. I'd use 2 or 3 data points per slope estimate.

3) Apply a zero slope threshold detector, i.e. discard points corresponding to the absolute value of slope greater than some threshold. This threshold will have to be varied until the standard deviation of the selected points is small enough.

4) Find the mean of the selected points as your estimated weight.

The mathematica code (ignore the initial period used for spacing.)
Code:
```.         (* weight data *)
weights =
{-5, 0, 10, 20, 40, 50, 70, 75, 76, 76, 77, 76, 75,
71, 54, 32, 20, 10, 0, 0} // N;
zeroSlopeThresh = 3;
slopeDetectorWindowLength = 3;

In=  (* add indices so we can do regression *)
weightData =
Riffle[
Array[# &, Length[weights]],
weights
];
weightData = Partition[weightData, {2}];

(* table of slope estimates *)
tt =
Table[
Normal[
LinearModelFit[
Take[
weightData,
{k, k + slopeDetectorWindowLength - 1}
],
x, x
]
]
[[2, 1]],
{k, 1, Length[weightData] - slopeDetectorWindowLength + 1}
];

(* select the weights corresponding to places
with small enough absolute value slope *)
selectedWeights =
Map[
weights[[#[]]] &,
Position[tt, x_/; Abs[x] < zeroSlopeThresh]
];

(* find the mean of the selected weights *)
averageWeight = Mean[selectedWeights];

(* find the standard deviation of the selected
weights *)
weightStdDeviation = StandardDeviation[selectedWeights];

(* display the results,
the rightmost number is how many weight measurements
were included *)
{averageWeight, weightStdDeviation,
Length[selectedWeights]}

Out= {76., 0.707107, 5}```
I do not know mathematica so I can't translate this to C. 7. I don't know if this is a homework or not.
I have a working code written in 2 hours but i don't post it (because homeworkpolicy).
The (verbose) output is:
Code:
```\$ ./measurement
Sample  0    .      * ->  -5
Sample  1    .     -5 ->   0
Sample  2    .      0 ->  10
Sample  3    .     10 ->  20
Sample  4    .     20 ->  40
Sample  5    .     40 ->  50
Sample  6    .     50 ->  70
Sample  7    .     70 ->  75
Sample  8 start!!  75 ->  76
Sample  9 found!!  76 ->  76
Sample 10 found!!  76 ->  77
Sample 11 found!!  77 ->  76
Sample 12 found!!  76 ->  75
Sample 13   ...    75 ->  71
Sample 14   ...    71 ->  54
Sample 15   ...    54 ->  32
Sample 16   ...    32 ->  20
Sample 17   ...    20 ->  10
Sample 18   ...    10 ->   0
Sample 19   ...     0 ->   0
--------------------
First Sample:  7
Last  Sample: 12
--------------------
Sample 7: 75
Sample 8: 76
Sample 9: 76
Sample 10: 77
Sample 11: 76
Sample 12: 75
--------------------
Result: 75.833333```
How those the code work?
It compare 2 samples and check if the gap isn't greater than the tolerated delta (in this case delta = 2).
The code found samples 7 and 8 have only a gap of 1 (start) and continuous to sample 12.
After sample 12 the gap is rising and fall out of range.
There is one more check (that you don't can see here) if there was values above the founded range.
In this case, the threshold_min and threshold_max will increased, the loop variable will be rewinded and the search start with the new min and max.
I have cheched it with altering value 9 to 78.
Output:
Code:
```\$ ./measurement
Sample  0    .      * ->  -5
Sample  1    .     -5 ->   0
Sample  2    .      0 ->  10
Sample  3    .     10 ->  20
Sample  4    .     20 ->  40
Sample  5    .     40 ->  50
Sample  6    .     50 ->  70
Sample  7    .     70 ->  75
Sample  8 start!!  75 ->  76
Sample  7 reset!!  70 ->  75
Sample  8 new  !!  75 ->  76
Sample  9 found!!  76 ->  78
Sample 10 found!!  78 ->  77
Sample 11 found!!  77 ->  76
Sample 12   ...    76 ->  75
Sample 13   ...    75 ->  71
Sample 14   ...    71 ->  54
Sample 15   ...    54 ->  32
Sample 16   ...    32 ->  20
Sample 17   ...    20 ->  10
Sample 18   ...    10 ->   0
Sample 19   ...     0 ->   0
--------------------
First Sample:  8
Last  Sample: 11
--------------------
Sample 8: 76
Sample 9: 78
Sample 10: 77
Sample 11: 76
--------------------
Result: 76.750000```
I think with my statements you have enough ideas to write your own code  8. Algorism, I also could not translate, I installed Wolfram but I need to study better ...

WoodSTokk, thanks for the help.

Currently the system already works similarly, it is informed a delta and checked the values that are inside it.
However, I would not like to inform a delta, use another formula, or identify the best delta ...

Sometimes the values in the center of the curve do not come so close (example: 70 72 75 77 80) Popular pages Recent additions belt, identify, piece, weight, weights 