• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Differentiate sine wave using microcontroller

Status
Not open for further replies.

itzme

New Member
Hi all,
I need your advice on something. Im currently working on a school project. It requires me to differentiate sine wave of different frequency using assembly language for the PIC. The sine wave is input into ADC to be converted into binary values. then the binary values will be input into the PIC. Is there anyway i can differentiate the two frequency? say one is 500Hz and another one is 5kHz?
Thanks
 

rainbow

New Member
i think u want to know which sin wave has higher frequency than the other
i think u can store data from first signal then u compare first reading input with other coming inputs .. try to get the second reading having the same value as the first reading (by comparing each new input with the first reading and checking the zero flag ) ... make sure u have a counter incrementing each new input

repeat for the second signal .
you have then two number ( counter of signal 1 and counter of signal 2)
compare between them .. the counter having more counts is the lower frequency and vice versa .
to get the period of the signal u must have the sampling frequancy
T=counter / sampling frequency
frequency = 1/T
u must have the same sampling frequancy for both signals
 

itzme

New Member
can i know the code for the counter? im not too sure how do i link the increment of the counter with the input. Thanks alot.


i think u want to know which sin wave has higher frequency than the other
i think u can store data from first signal then u compare first reading input with other coming inputs .. try to get the second reading having the same value as the first reading (by comparing each new input with the first reading and checking the zero flag ) ... make sure u have a counter incrementing each new input

repeat for the second signal .
you have then two number ( counter of signal 1 and counter of signal 2)
compare between them .. the counter having more counts is the lower frequency and vice versa .
to get the period of the signal u must have the sampling frequancy
T=counter / sampling frequency
frequency = 1/T
u must have the same sampling frequancy for both signals
 

rainbow

New Member
i have no code now
but u must know the sampling rate so u know exactly when a new data is at the port .
new data can be saved in a register and then u can increment the counter.
...............did i answer your question ?
 

itzme

New Member
i have no code now
but u must know the sampling rate so u know exactly when a new data is at the port .
new data can be saved in a register and then u can increment the counter.
...............did i answer your question ?

the sampling rate for my project is 100khz. does it mean at every interval of 10us im suppose to store a value into the register? or just the first value? how do i test if there is value coming into the port? thanks
 

rainbow

New Member
the sampling rate for my project is 100khz. does it mean at every interval of 10us im suppose to store a value into the register? or just the first value?
u can save it or simply put it in the work register just to do your comparison

how do i test if there is value coming into the port? why u test it you have always a value into the port , but it is varying each 10us so u must get it each 10us
u can save it or simply put it in the work register just to do your comparison.
 

Hero999

Banned
Sorry but it's a very silly idea.

Just use an op-amp, resistor and capacitor.

If your lecturer whines at you, you can bias a comparator in linear mode so it behaves like an op-amp, then add the resistor and capacitor. :D
 
Last edited:

Calico Jack

New Member
Or

If you just need to know if if the input Freq. is NOT the same as last time. Use the touch capitor algarithim.

Microchip mTouch Capacitive Touch Sensing Product Training Module provided by Digi-Key and Microchip.

You need to make the Sine a Square. I think it would be easy to use a op amp.

OPERATIONAL AMPLIFIER BASICS by Harry Lythall

You want the frequency mesurment part. Super easy..well mostly. That should do it. O and if you sample it fast enough you can use it for Frequency-shift keying (FSK). Wiki that.

So there you go. Have fun!

_______________________________________________________________
If you can't Hack it. You don't own it.
 

dougy83

Well-Known Member
Is there anyway i can differentiate the two frequency? say one is 500Hz and another one is 5kHz?
So is there only ever 1 frequency at a time? or more?

If there's only one, you can easily get the frequency by implementing a zero-crossing detector (with a bit of hyteresis). Then for a set number of samples you can count the zero crossings (or for a set number of zero crossings you can count the number of samples) - from this you can calculate the frequency.

The 'zero' is of course the mid-point/average of the signal. You don't need to store the actual ADC samples for this.
 

itzme

New Member
So is there only ever 1 frequency at a time? or more?

If there's only one, you can easily get the frequency by implementing a zero-crossing detector (with a bit of hyteresis). Then for a set number of samples you can count the zero crossings (or for a set number of zero crossings you can count the number of samples) - from this you can calculate the frequency.

The 'zero' is of course the mid-point/average of the signal. You don't need to store the actual ADC samples for this.
yes. there is only one frequency at a time. Do u mean i check the zero crossing of the frequency when it cut's at zero? Say im using a 5kHz signal, so at time 200us i check for zero crossings to determine what frequency is it? Do i need to use an eeprom to perform such function?
 

dougy83

Well-Known Member
Do u mean i check the zero crossing of the frequency when it cut's at zero?
The 'zero' point of an sinewave is it's mid point. So you check when the signal 'cuts' or passes it's mid point. You may like to include hysteresis to improve noise immunity.

Say im using a 5kHz signal, so at time 200us i check for zero crossings to determine what frequency is it?
No. You don't know what the frequency will be. You said the ADC samples at 100kHz, so you check for crossings at 100kHz (i.e. every 10us).

Do i need to use an eeprom to perform such function?
No
 
Last edited:

itzme

New Member
The 'zero' point of an sinewave is it's mid point. So you check when the signal 'cuts' or passes it's mid point. You may like to include hysteresis to improve noise immunity.

No. You don't know what the frequency will be. You said the ADC samples at 100kHz, so you check for crossings at 100kHz (i.e. every 10us).

No
Im sorry but can i know how do i include hysteresis in the code? thanks.
Oh ok. So even if I have 5 sinewaves of different frequencies, the zero crossing at 100kHz will always be zero?
 

dougy83

Well-Known Member
Im sorry but can i know how do i include hysteresis in the code? thanks.
e.g. if the ADC is 8 bit, range is 0..255 units. Midpoint is 127. Hysteresis chosen to be +/- 5 units...

Code:
   byte adc = getAdc()

   // hysteresis to get a 1 for high, or a 0 for low
   if adc > (127 + 5) then
      state = 1
   elseif adc < (127 - 5) then
      state = 0
   endif
the value in the variable 'state' will now be 0 or 1 depending on whether the signal is low or high. That code takes into account the crossing and the hysteresis. You now just have to check when the result changes from 0 to 1 or from 1 to 0. Count the number of changes. The signal frequency can be derived from this count for a given number of 10us samples.

Oh ok. So even if I have 5 sinewaves of different frequencies, the zero crossing at 100kHz will always be zero?
Duh.. what? The crossing point will be at the crossing point.

The 100khz sample rate means that the ADC is 'looking' at the instantaneous value of the signal 100000 times every second. If your signal is slower than this sampling/'looking' rate then what the ADC 'sees' won't change much between each sample/'look'. When you see that the value crosses the point you have set (generally the midpoint of the signal), it is counted as a crossing.
 
Last edited:
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top