# Measuging signals with dsPIC30F2010

#### Mithun_K_Das

How can I measure the average points of the peak if these signals and one avg of max&min value of sine wave. Note that I'm new with dsPIC ics but have been working with PIC and atmel ICs for a long time. Yes, I can do it in my older method but those are time consuming for this product. That is why I'm asking for suggestion from experienced friends who did similar work with dsPIC.

Using dsPIC30F2010. Compiler: mikroC pro for dsPIC v7.1.0
There is a small dc voltage in each signal which can not be eliminated from these signals with circuits.

Kindly do not ask for the circuit diagram. The circuit is a ready made device which is not my importance, I'm just trying to do the same working code of that product in my way. I measured the signals in ADC pins and mentioned in the image. There are 4 different signals to measure.

Any suggestion or small example will be greatly helpful to me.

#### KlausST

##### Super Moderator
Staff member
Hi,

Indeed .. a schematic should be useful to validate if it is possible at all and what errors one has to expect.

Yes, I can do it in my older method but those are time consuming for this product
This sentence is useless, because we neither know your "older method" nor your expectation of timing.

You don't give details at all. Thus it is hard to help.

In my eyes the problem is not related to dsPIC, it rather is related to
* signal timing
* used analog filters

And on the expected output
* update rate
* resolution
* precision
* continous processing (real time sampling and prcessing) or discontinous processing (first sample a lot of data, then process the sampled data)

Also you should be more exact about "averaging".
* often "averaging" is made with a low pass filter (like RC)
* but it can also be "true averaging" over a specified time
* but it can also be "true averaging" over a specified number of peaks

You should be aware of all this before you start coding. I usually take a piece of paper and a pencil and draw some pictures of the signals and the code flow chart.

******
My recommendations:
* use an analog filter to get rid of noise = to focus on the "true" signal
* use a sampling rate at least 25 times the signal frequency. In detail it depends on expected precision and waveform
* use constant and known samling frequeny
* use a (running) average signal, or a low pass filterd signal ... over all samples ... as helping signal to validate the input signal.
* use this signal to detect whehet the input signal is valid (in amplitude as well as in frequency), and to detect in which halfwave you currently are
* define a positive halfwave when n input_samples > average signal
* define a negative halfwave when n input_samples < average signal
* every time when you detect a positive halfwave: save the negative_peak and then set it to the average value
* every time when you detect a negative halfwave: save the positive_peak and then set it to the average value
* ... here you need to specify output rate
* now calculate the average of the saved positive peaks
* now calculate the average of the saved negative peaks
To define whether the input signal is symmetric (top right picture) I'd compare (pos_avg_peak - average value) with (average_value - neg_avg_peak).
If both are about the same value (you first need to define your tolerance), then you may consider the input signal as symmetric.
--> top right picture with x and y values
If not: --> other picture with just x values

Klaus

Mithun_K_Das

### Mithun_K_Das

points: 2

#### Mithun_K_Das

Signal frequency: 50Hz; max ac peak signal: 3.1V. Sample rate: 1MSPS.

If I can find the difference of two peak points (the maximum and the minimum points) then everything else can be solved. I'm trying this.

The circuit is a ready made, they used same MCU(dsPIC30F2010) which is working fine and measuring every signal perfectly. I'm trying to do the code in my own way, completed all control works, just need to sense the signals parfectly. I'm trying all of your suggestions.

#### KlausST

##### Super Moderator
Staff member
Hi,

Mains frequency is 50 Hz, thus you have 20ms period time.
If you use 1MSample/s this means 20.000 samples per period ...in my eyes this is overkill.

Calculation example:
At a clean full scale sine, sampled with 10bits resolution it takes 0.2ms at the top to change 1LSB.
Now this still means 200 (useless) samples @ 1MSampl/s.
For sure you don't have a clean sine shape ... thus you will need a higher rate than 1/0.2ms = 5kHz. But 200 times higher? We don't know.
When you aim for higher frequency signals .. it rather seems you look for "noise" or "spikes" rather than sine related values.
Is this the job of your application?

Klaus

#### Mithun_K_Das

Yes, the system is measuring those signals. I don't know how they did it. but they did it. I've to find the way.

- - - Updated - - -

As DSP (digital signal processor) have some build in function, is there anything in those features which can solve the problem?

- - - Updated - - -

Code:
unsigned int sample_recent=0,sample_max=0,sample_min=0,sample_avg_point,sample_avg_point_max;
int match_cnt=0;
{
sample_recent=0;
for(k=0;k<20;k++)
{
}
sample_recent/=20;

if(sample_recent>sample_max && sample_recent>sample_avg_point)
{
sample_max=sample_recent;
}
else if(sample_recent<sample_min && sample_recent<sample_avg_point)
{
sample_min=sample_recent;
}
else
{
sample_avg_point = (sample_max-sample_min)/2;
if(sample_avg_point>sample_avg_point_max)
{
sample_avg_point_max = sample_avg_point;
match_cnt--;
}
if(sample_avg_point==sample_avg_point_max,sample_avg_point>200) // average point is valid and have some minimum value
{
match_cnt++;
if(match_cnt>10)
{
output_voltage=(sample_max-sample_min); //final result
sample_max=0;
sample_min=0;
sample_avg_point_max=0;
match_cnt=0;
}
}
}
}

almost stable result. Give me your suggestion to purify it.