I am changing the phase of signal from 0 to 360 by each degree to get max voltage value.Because if i change phase of the signal the voltage also changes.I have the fallowing code to find max value. and corresponding phase

``````void Maxphase(float *max, unsigned int *index)
{
*max = 0.0;
float value;
unsigned int i, data;
for (i=0;i<=360;i++)
{
phaseset(i);
delay_ms(100);
value = voltage(mux1);
if(value > *max)   //find max value
{
*max = value;    //max voltage
*index = i;
}
}
}
``````

from the above code I am getting Max value(voltage) after 38 sec(360*100) because for every read operation my device needs 100ms delay. This is too large, I can't change hardware thus i want to get the max value within 2 to 3 sec by optimizing software.
then I have tried with the fallowing code.

`````` void Maxphase(float *max1, unsigned int *index1)
{
max = 0.0;
float value;
unsigned int i,j,data;
for (i=0;i<360;i+=10)
{
phaseset(i);
delay_ms(100);
value = voltage(mux1);
if(value > max)   //find max value
{
max = value;    //max voltage
index = i;
}
}
*max1=max;
*index1=index;
for (i=*index1-9;i<=*index1+9;i+=1)
{
j=i;
phaseset(j);
delay_ms(100);
value = voltage(mux1);
if(value > *max1)   //find max value
{
*max1 = value;    //max voltage
*index1 = i;
}
}
}
``````

I have reduced time from 45 sec to 7 sec. i have reduced iterations 360 to 54(54*100). I want to reduce it 7 sec to 2 sec.

Can any one help me with better program that i can get max value from (0 to 360) with in 2 to 3 sec.

I have measured the voltage values using scope by changing phase. I have written below how it vary voltage with phase.

``````Phase (degree)     voltage(max)
0             0.9mv

45             9.5mv

90             9.0mv

135             0.9mv

180             292mv

225             601mv

270             555mv

315             230mv

360             0.9mv
``````

I am new to C programming. Can anyone provide sample code.

## All 8 Replies

This is a lot like a binary search.
Looking at this, you can already tell where to look after 4 readings.
So drop it to 4 readings: 0,90,180,270 (you already know 0 and 360 are the same.)
Pick exactly in the middle.
Repeat.

can you provide a smaple code, if possible.

I apologize that I haven't gotten this to you yet. I got halfway through then realized the data (being somewhat symmetrical) can be misleading. I'm still working on it. I'll test it Saturday and Sunday.

Happy coding!

Dean, he's posted this thread on at least 3 programming boards, and received code and the most detailed software and hardware discussion on another board.

He's had at least 4 algorithms and several snippets of code given to him, already. He wants code - and he's been given code, but doesn't want to work with it, apparently.

If the voltage at the phase angles were to stay contant for the angle, then it becomes a simple matter of testing some values, sorting the values you recieve, and a binary search would be very fast look up to find the perfect phase angle.

But that's not the case. He needs minimum testing, since he needs the answer within 2 seconds, and each test takes 100 milliseconds. So the question becomes, what's the stability of voltage to phase angle, over time? Is it consistent, or does it vary? And by how much?

He's been advised to have the tester changed to continuous testing mode, which is much faster, after looking at the data for the tester he has. He says the hardware "guy" is reluctant or unwilling to make that change.

Without consistency info, you can't design the absolute best algorithm for the software, but he's been given several excellent one's, including code for some of them.

Go to CBoard C forum, for that discussion. I wouldn't spend more time on it, although the problem is intriguing.

But that's not the case. He needs minimum testing, since he needs the answer within 2 seconds, and each test takes 100 milliseconds. So the question becomes, what's the stability of voltage to phase angle, over time? Is it consistent, or does it vary? And by how much?

I am generating the signal and giving it as input to sensor and taking sensor output.I am selecting both the signals by using MUX. The ouput signal is phase shifted by some degree to the input. I am using the capacitive sensor so the phase shift depends on the medium of capacitor with respect to time. The phase shift will change if medium chamges for example Air or water or some other medium pass through the capacitor then Phase will change otherwise it will be constant. I want to measure that phase diffrence between two signals at every 5 sec, as well as voltages.

But the question remains :"Is there a consistent relationship between phase angle and voltage?", because these other things you have mentioned, are not all relevant to creating a best software algorithm.

I agree completely that hardware can do all this a lot faster, but that might be expensive and may not even be practical, I'm not sure. For a good software algorithm, we need more of the relavent info.

In your hardware, what is the relationship between the phase angle and voltage? Does it vary? By how much?

If there is a good relationship between phase angle and voltage consistently, then which is easier to read for you? If it's consistent, we can get data from several runs, and see clearly what is the best setting.

Otherwise, we are making suggestions that are sub optimal.