Frequency to Period Conversion

Status
Not open for further replies.

uranyumx

Advanced Member level 4
Joined
Mar 5, 2011
Messages
102
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,296
Activity points
2,095
Hello,

I collected a certain number of frequency values in an array (temp). In another array(tempPeriod), I want to store the period equivalents of each frequency values of temp array in microseconds. For example, the idea should be tempPeriod = (1000000*(1/temp)); But I got zero from tempPeriod result.

Do you have any suggestions?

Thank you,

Code:
uint32_t temp [100];
uint32_t tempPeriod [100];
uint16_t fMinModulation = 0;
uint16_t fDeltaModulation = 0;

for (int i = 0; i < numofpulse; i++)
    temp[i] = fMinModulation + (i*fDeltaModulation);
    tempPeriod[i] = (1000000*(1/temp[i]));
 

Hello!

From the code you are posting, you have 2 values initialized to 0.
Therefore first of all, the following line:

fMinModulation + (i*fDeltaModulation)

will translate to

0 + i * 0

which is not much.

That said, the runtime should blow up at the next line, because 1/temp
consequently translates to 1/0.

Is this your real code? Or are the snippets taken from 2 parts in the program,
and in between, temp is initialized to some non-zero values?

Supposing it's the case and all your temp values are non-zero: you
are using integer values, and therefore you are performing integer division.
This means that for any temp which is more than 1, the terme 1/temp will be
less than 0.5 which yields 0 in integer values.
But if you calculate 1000000/temp instead, you will get non-zero values.

Now beside this, your code is not safe. You should always test when you do a
division. What do you want to do if temp = 0?

something like:


Code C++ - [expand]
1
2
3
4
5
6
if(temp > 0) {
    tempPeriod = 1000000/temp;
}
else {
   // Write your B plan here.
}



Dora.
 

1/int is zero for all ints. (except zero)

1000000*0 = 0

Change your code to:

tempPeriod = 1000000/temp
 
Reactions: FvM

Solution
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…