Thesis 7th Semester

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 49

Digital Phase Sensitive Detectors

By
Aneela Riaz
Asma Mohsin
Faiz Ahmed

Thesis submitted to the Faculty of Engineering at PIEAS in partial fulfillment of


requirements for the Degree of B.S. Electrical Engineering

Department of Electrical Engineering


Pakistan Institute of Engineering & Applied Sciences Nilore,
Islamabad, Pakistan
January, 2022
Declaration of Originality
We hereby declare that the work contained in this thesis and the intellectual content of this thesis are the
product of our own work. This thesis has not been previously published in any form nor does it contain
any verbatim of the published resources which could be treated as an infringement of the international
copyright law.

We also declare that we do understand the terms ‘copyright’ and ‘plagiarism’, and that in case of any
copyright violation or plagiarism found in this work, we will be held fully responsible for the consequences
of any such violation.

Signature: ___________________________

Name: ______________________________

Signature: ___________________________

Name: ______________________________

Signature: ___________________________

Name: ______________________________

Date: ____________________

Place: ____________________
Certificate of Approval
This is to certify that the work contained in this thesis entitled

“Digital Phase Sensitive Detector”


was carried out by
Aneela Riaz, Asma Mohsin & Faiz Ahmed
Under our supervision and that in our opinion, it is fully adequate, in scope and
quality, for the degree of B.S. Electrical Engineering from Pakistan Institute of
Engineering and Applied Sciences (PIEAS).

Approved By:

Signature: ________________________
Supervisor: Mr. Zeeshan Alvi

Signature: ________________________
Co-Supervisor: Dr. Muhammad Aqil

Verified By:

Signature: ________________________
Head, Department of Electrical Engineering
Stamp:
To our Friends and family
Thank you for all your Support along the way.
Acknowledgement
First of all, all praise to ALLAH for bestowing us with knowledge and blessings. All respect to
Prophet Muhammad (peace be upon him) for their kindness to this world and hereafter. This Thesis
would have been possible with the assistance, support and supervision of many people. For
keeping us focused, believing in us, and offering the project in the first place, we are very thankful
to our Supervisor, Mr. Zeeshan Alvi and Co-Supervisor Dr. Muhammad Aqil who always kept us
motivated and provided firm support for carrying this project. We express our sincere gratitude to
Mr. Zeeshan Alvi in understanding of basic concepts and theory of Digital Signal Processing.
Further, we acknowledge our gratitude to our parents who have always been a source of inspiration
and motivation for us during all our endeavors.

Aneela Riaz

Asma Mohsin

Faiz Ahmed
Table of Contents
1. Why Phase Sensitive Detectors? ........................................................................................................................7
1.1 Introduction ................................................................................................................................................7
1.2 Problems ...................................................................................................................................................10
1.2.1 Harmonic Response .........................................................................................................................11
2. Mathematical Modeling ....................................................................................................................................13
2.1 How it works? ...........................................................................................................................................13
2.1.1 Rectangular pulse ............................................................................................................................13
2.1.2 Sine pulse ..........................................................................................................................................15
2.2 What if frequency of our reference signal is different from the fundamental frequency of our input
signal? 16
2.2.1 Rectangular pulse ............................................................................................................................16
2.2.2 Sine pulse ..........................................................................................................................................17
3. Sampling Rate- Nyquist and Shannon Criteria ..............................................................................................19
3.1 Effects of ignoring Nyquist Criteria ..............................................................................................................19
3.2 Using Arduino Uno to determine the effects of the Sampling Rate ............................................................21
3.3 Arduino Code with Explanation described by comments given below .......................................................22
4. Software Modeling ............................................................................................................................................24
4.1 Introduction ..............................................................................................................................................24
4.2 Modeling with Interpolation ...................................................................................................................24
4.2.1 Code ..................................................................................................................................................24
4.2.2 Data from Oscilloscope ...................................................................................................................39
4.2.3 Code ..................................................................................................................................................39
5. How the Filter Works .......................................................................................................................................40
5.1 Brief Description ......................................................................................................................................40
5.1.1 Code ..................................................................................................................................................41
6. Appendix ............................................................................................................................................................43
6.1 Comparison between Look-Up Table and Interpolation ............................................................................43
6.2 Pure SINE and COSINE Wave ..................................................................................................................43
6.3 SINE and COSINE using Look-Up Table .................................................................................................44
6.4 SINE and COSINE using Interpolation......................................................................................................46
6.5 Results .......................................................................................................................................................48
1. Why Phase Sensitive Detectors?
1.1 Introduction
Precision electronic measurements are ubiquitous in modern physics. Experimental physicists are
frequently faced with the challenge of measuring extremely small electronic signals from any
number of sources. As the Phase Sensitive detectors is a solution to a measurement problem, we
can usefully describe its action and composition by looking at the sort of problems that occur when
light measurements are pushed to the limit.

To measure transmission let’s consider a simple example of Light measuring system. Light from
a stable light source is passed through a system and then passed to detector. Measurements from
the detector is further amplified and displayed on the meter. The meter reading gives an indication
of the amount of light amplified by the sample.

Figure 1 Light Measuring System

It is noticeable that as the signal level falls and the amplifier gain is increased so the precision with
which the results can be recorded also falls. This is due to noise. In signal processing, noise is
unwanted signal that contributes to the meter reading other than the parameter being measured.
There are different types of noises such as White noise. Additive white Gaussian noise, Black
noise, Gaussian noise and Pink noise or flicker noise, with 1/f power spectrum. In some cases the
noise we are faced with is pure white noise, also called Gaussian noise. For this type of noise,
Vnoise(t) fluctuates randomly and rapidly with time in such a way that the statistics of the noise are
independent of time, and each value of Vnoise(t) is completely uncorrelated with Vnoise(t`), provided
that | t-t`| is greater than some correlation time t.
In the real world, however, noise is seldom white. Amplifiers and other noise-generating elements
often drift slowly with time. Over short periods the output of a typical amplifier will drift a small
amount, and over long times it will drift a larger amount. Some of this comes from environmental
effects—temperature drifts in the lab, creep in materials, etc., but some is intrinsic to many
electronic devices. Frequency spectrum of these kinds of noise sources is often approximately
proportional to 1/f, where f is the frequency. Many electronic devices exhibit this ‘‘1/f noise’’ even
at very low frequencies.
Noise in this light measuring system, it is generally dominated by the noise from the detector or
noise associated with light signal. Figure 2 shows how noise affects the signal to noise ratio. This
describes the situation where the density of the sample is so high that the signal is smaller than the
noise with the result is that the instrument becomes unusable.

At higher frequencies the spectrum flattens out to give a reasonably constant shot noise
background which is associated with the quantum nature of light. Electrical interference from the
power system produces small peaks at 50Hz and 150Hz. The larger peak at 100Hz is due to light
from room lighting leaking into the instrument and reaching the detector.

We can use electronic filter to reject higher frequency components which do not contain any signal
information. As information related to the sample of transmission is near 0Hz so using a low pass
filter say 30Hz will not give significant impact to improve signal to noise ratio (SNR). Figure
below shows the impact of electronic filter on SNR.

We have to move signal information from 0Hz line in order to separate it from high density noise.
Simple method is to place optical Chopper between optical source and detector which will

Figure 2 Optical Measuring System with Chopper


periodically interrupt the light. Diagram below shows chopper placed in-between light source and
sample.

Chopper is running at 175Hz and a band pass filter is placed ahead of amplifier. In the result, we
have moved the signal away from a region where the background noise is high to a region where
it is low. We can now pass the signal through an electronic band-pass filter which will reject both
the noise at higher and lower frequencies including zero hertz and hence significantly improve the
signal to noise ratio.

Figure 3 Band Pass filter effect at 175Hz

1.2 Problems
Tuned amplifiers used in this example suffer 3 major problems:

In this application the narrower the bandwidth of the filter, the greater is the noise rejection so our
main concern is central frequency Q. As central frequency define bandwidth of the filter. The
maximum Q typically achievable for a tuned amplifier is in the region of 100, but in a
demanding measurement situation we might need a Q of 1000 to achieve acceptable signal to
noise ratio. Another problem is that any change in chopping frequency results in large changes in
output due to misalignment between signal frequency and filter center frequency. Using rectifying
noise device rectifies noise but it will also give rise to dc level which is another major problem.
All these problems can be resolved by using Phase Sensitive Detectors.

Figure 4 Synchronous Filter

1.2.1 Harmonic Response


In the multiplier model of our phase sensitive detector we made the assumption that the two
waveforms were sinusoidal, but in practice, when using a simple switch we are effectively
multiplying the signal not by a sine wave but by a square wave (the switch is either closed or open).
Now a square wave is equivalent to a sine wave of the fundamental frequency plus all the odd
harmonics of the fundamental. A PSD using a simple switch therefore behaves as a filter
centered at the fundamental but with windows at each odd harmonic.
Figure 5 PSD Frequency Response
2. Mathematical Modeling

Digital Phase Sensitive Detector (PSD) is a device that is used to measure very small AC signals
even up to few nanovolts. These signals are concealed by noise which can be even thousands of
time larger than our desired signal. Now PSD singles out the component of the signal at specific
reference frequency and phase. Noise only at that specific reference frequency is considered while
at all other frequencies it is rejected. It thus increases signal to noise ratio (SNR) so that we can
detect the signal correctly.

2.1 How it works?


An input signal is given to our design through the Signal In node. After this we have a low noise
differential amplifier which is used to reduce external interference. Then we multiply the input
signal with a gain to increase its magnitude however note that noise is also multiplied with the
same gain and thus enhanced. Next a local oscillator is used that generates cosine wave and also
passes it through a 90 degrees phase shift register to generate a sine wave. At this point we have a
sine and cosine wave and 2 multipliers and also the input signal. What we do next is that one input
to each multiplier is our signal after the gain block while the other input is cosine in one case and
sine in the other.

Let us consider an example where we have a rectangular pulse at input. a rectangular pulse is
made of a series of cosine waves at different frequencies. We first multiply it with a sine wave and
do the following calculations

2.1.1 Rectangular pulse


𝑆𝑖𝑔 = 𝐴 𝑠𝑖𝑛(𝜔𝑡 + 𝜃𝐴 ) + 𝐵 sin(2𝜔𝑡 + 𝜃𝐵 ) + 𝐶 sin(3𝜔𝑡 + 𝜃𝐶 ) + … … ..
𝑟 = cos(𝜔𝑡 + 𝜑 )

Sig ∗ r = 𝐴 𝑠𝑖𝑛(𝜔𝑡 + 𝜃𝐴 ) ∗ cos(𝜔𝑡 + 𝜑 ) + 𝐵 sin(2𝜔𝑡 + 𝜃𝐵 ) ∗ cos(𝜔𝑡 + 𝜑 ) + 𝐶 sin(3𝜔𝑡 + 𝜃𝐶 )


∗ cos(𝜔𝑡 + 𝜑 ) + … … ..
Figure 6 Block Diagram

𝟏
𝒔𝒊𝒏 ∝ ∗ 𝐜𝐨𝐬 𝜷 = [𝐬𝐢𝐧(𝜶 + 𝜷) + 𝐬𝐢𝐧(𝜶 − 𝜷)]
𝟐
𝐴
𝑆𝑖𝑔 ∗ 𝑟 = [sin(𝜔𝑡 + 𝜃𝐴 + 𝜔𝑡 + 𝜑 ) + sin(𝜔𝑡 + 𝜃𝐴 − 𝜔𝑡 − 𝜑 )]
2
𝐵
+ [sin(2𝜔𝑡 + 𝜃𝐵 + 𝜔𝑡 + 𝜑) + sin(2𝜔𝑡 + 𝜃𝐵 − 𝜔𝑡 − 𝜑)]
2
𝐶
+ [sin(3𝜔𝑡 + 𝜃𝐶 + 𝜔𝑡 + 𝜑) + sin( 3𝜔𝑡 + 𝜃𝐶 − 𝜔𝑡 − 𝜑)] + … … … ..
2

𝐴
𝑆𝑖𝑔 ∗ 𝑟 = [sin(2𝑤𝑡 + 𝜃𝐴 + 𝜑) + sin(𝜃𝐴 − 𝜑 )]
2
𝐵
+ [sin(3𝜔𝑡 + 𝜃𝐵 + 𝜑 ) + sin(𝜔𝑡 + 𝜃𝐵 − 𝜑)]
2
𝐶
+ [sin(4𝜔𝑡 + 𝜃𝐶 + 𝜑) + sin(2𝜔𝑡 + 𝜃𝐶 − 𝜑)] + … … …
2

After this we apply a LPF and get

𝑨
𝑿 = 𝑺𝒊𝒈 ∗ 𝒓 = 𝐬𝐢𝐧(𝜽𝑨 − 𝝋)
𝟐
We have obtained our signal X at this point. It is a DC signal since there is no component in sine
that is varying. Next we multiply our input signal with a sine wave and get the following
calculations

2.1.2 Sine pulse


𝑆𝑖𝑔 = 𝐴 𝑠𝑖𝑛(𝜔𝑡 + 𝜃𝐴 ) + 𝐵 sin(2𝜔𝑡 + 𝜃𝐵 ) + 𝐶 sin(3𝜔𝑡 + 𝜃𝐶 ) + … … ..

𝑟 = sin(𝜔𝑡 + 𝜑 )

Sig ∗ r = 𝐴 𝑠𝑖𝑛(𝜔𝑡 + 𝜃𝐴 ) ∗ sin(𝜔𝑡 + 𝜑 ) + 𝐵 sin(2𝜔𝑡 + 𝜃𝐵 ) ∗ sin(𝜔𝑡 + 𝜑 ) + 𝐶 sin(3𝜔𝑡 + 𝜃𝐶 )


∗ s 𝑖𝑛(𝜔𝑡 + 𝜑 ) + … … ..

𝟏
𝒔𝒊𝒏 ∝ ∗ 𝐬𝐢𝐧 𝜷 = [𝐜𝐨𝐬(𝜶 − 𝜷) − 𝐜𝐨𝐬(𝜶 + 𝜷)]
𝟐
𝐴
𝑆𝑖𝑔 ∗ 𝑟 = [cos(𝜔𝑡 + 𝜃𝐴 − 𝜔𝑡 − 𝜑 ) − cos(𝜔𝑡 + 𝜃𝐴 + 𝜔𝑡 + 𝜑 )]
2
𝐵
+ [cos(2𝜔𝑡 + 𝜃𝐵 − 𝜔𝑡 − 𝜑) − cos(2𝜔𝑡 + 𝜃𝐵 + 𝜔𝑡 + 𝜑)]
2
𝐶
+ [cos(3𝜔𝑡 + 𝜃𝐶 − 𝜔𝑡 − 𝜑) − cos( 3𝜔𝑡 + 𝜃𝐶 + 𝜔𝑡 + 𝜑)] + … … … ..
2

𝐴
𝑆𝑖𝑔 ∗ 𝑟 = [cos(𝜃𝐴 − 𝜑 ) − cos(2𝑤𝑡 + 𝜃𝐴 + 𝜑)]
2
𝐵
+ [cos(𝜔𝑡 + 𝜃𝐵 − 𝜑) − cos(3𝜔𝑡 + 𝜃𝐵 + 𝜑 )]
2
𝐶
+ [cos(2𝜔𝑡 + 𝜃𝐶 − 𝜑) − cos (4𝜔𝑡 + 𝜃𝐶 + 𝜑)] + … … …
2

After LPF we get

𝑨
𝒀 = 𝑺𝒊𝒈 ∗ 𝒓 = 𝐜𝐨𝐬(𝜽𝑨 − 𝝋)
𝟐

Now after we have our X and Y signals we can find R by the formula

𝑹 = √𝑿𝟐 + 𝒀𝟐

And theta by the formula


𝒀
𝜽 = 𝐭𝐚𝐧−𝟏
𝑿

2.2 What if frequency of our reference signal is different from the


fundamental frequency of our input signal?
For this purpose we include a ∆𝜔𝑡 factor in our reference frequency and do the calculations
again. First we multiply our signal with a cosine wave and get

2.2.1 Rectangular pulse


𝑆𝑖𝑔 = 𝐴 𝑠𝑖𝑛(𝜔𝑡 + 𝜃𝐴 ) + 𝐵 sin(2𝜔𝑡 + 𝜃𝐵 ) + 𝐶 sin(3𝜔𝑡 + 𝜃𝐶 ) + … … ..

𝑟 = cos(𝜔𝑡 + ∆𝜔𝑡 + 𝜑 )

Sig ∗ r = 𝐴 𝑠𝑖𝑛(𝜔𝑡 + 𝜃𝐴 ) ∗ cos(𝜔𝑡 + ∆𝜔𝑡 + 𝜑 ) + 𝐵 sin(2𝜔𝑡 + 𝜃𝐵 ) ∗ cos(𝜔𝑡 + ∆𝜔𝑡 + 𝜑 )


+ 𝐶 sin(3𝜔𝑡 + 𝜃𝐶 ) ∗ cos(𝜔𝑡 + ∆𝜔𝑡 + 𝜑 ) + … … ..

𝟏
𝒔𝒊𝒏 ∝ ∗ 𝐜𝐨𝐬 𝜷 = [𝐬𝐢𝐧(𝜶 + 𝜷) + 𝐬𝐢𝐧(𝜶 − 𝜷)]
𝟐

𝐴
𝑆𝑖𝑔 ∗ 𝑟 = [sin(𝜔𝑡 + 𝜃𝐴 + 𝜔𝑡 + ∆𝜔𝑡 + 𝜑 ) + sin(𝜔𝑡 + 𝜃𝐴 − 𝜔𝑡 − ∆𝜔𝑡 − 𝜑 )]
2
𝐵
+ [sin(2𝜔𝑡 + 𝜃𝐵 + 𝜔𝑡 + ∆𝜔𝑡 + 𝜑) + sin(2𝜔𝑡 + 𝜃𝐵 − 𝜔𝑡 − ∆𝜔𝑡 − 𝜑)]
2
𝐶
+ [sin(3𝜔𝑡 + 𝜃𝐶 + 𝜔𝑡 + ∆𝜔𝑡 + 𝜑) + sin( 3𝜔𝑡 + 𝜃𝐶 − 𝜔𝑡 − ∆𝜔𝑡 − 𝜑)]
2
+ … … … ..

𝐴
𝑆𝑖𝑔 ∗ 𝑟 = [sin(2𝑤𝑡 + ∆𝜔𝑡 + 𝜃𝐴 + 𝜑) + sin(−∆𝜔𝑡 + 𝜃𝐴 − 𝜑 )]
2
𝐵
+ [sin(3𝜔𝑡 + ∆𝜔𝑡 + 𝜃𝐵 + 𝜑 ) + sin(𝜔𝑡 − ∆𝜔𝑡 + 𝜃𝐵 − 𝜑)]
2
𝐶
+ [sin(4𝜔𝑡 + ∆𝜔𝑡 + 𝜃𝐶 + 𝜑) + sin(2𝜔𝑡 − ∆𝜔𝑡 + +𝜃𝐶 − 𝜑)] + … … …
2
After LPF we get

𝑨
𝑿 = 𝑺𝒊𝒈 ∗ 𝒓 = 𝐬𝐢𝐧(𝜽𝑨 − ∆𝝎𝒕 − 𝝋)
𝟐

Note that here is an additional ∆𝜔𝑡 factor in our angle and thus our signal after LPF is not a DC
one. Next we multiply the input signal with the sine wave at different reference frequency.

2.2.2 Sine pulse


𝑆𝑖𝑔 = 𝐴 𝑠𝑖𝑛(𝜔𝑡 + 𝜃𝐴 ) + 𝐵 sin(2𝜔𝑡 + 𝜃𝐵 ) + 𝐶 sin(3𝜔𝑡 + 𝜃𝐶 ) + … … ..

𝑟 = sin(𝜔𝑡 + ∆𝜔𝑡 + 𝜑 )

Sig ∗ r = 𝐴 𝑠𝑖𝑛(𝜔𝑡 + 𝜃𝐴 ) ∗ sin(𝜔𝑡 + ∆𝜔𝑡 + 𝜑 ) + 𝐵 sin(2𝜔𝑡 + 𝜃𝐵 ) ∗ sin(𝜔𝑡 + ∆𝜔𝑡 + 𝜑 )


+ 𝐶 sin(3𝜔𝑡 + 𝜃𝐶 ) ∗ s 𝑖𝑛(𝜔𝑡 + ∆𝜔𝑡 + 𝜑 ) + … … ..

𝟏
𝒔𝒊𝒏 ∝ ∗ 𝐬𝐢𝐧 𝜷 = [𝐜𝐨𝐬(𝜶 − 𝜷) − 𝐜𝐨𝐬(𝜶 + 𝜷)]
𝟐

𝐴
𝑆𝑖𝑔 ∗ 𝑟 = [cos(𝜔𝑡 + 𝜃𝐴 − 𝜔𝑡 − ∆𝜔𝑡 − 𝜑 ) − cos(𝜔𝑡 + 𝜃𝐴 + 𝜔𝑡 + ∆𝜔𝑡 + 𝜑 )]
2
𝐵
+ [cos(2𝜔𝑡 + 𝜃𝐵 − 𝜔𝑡 − ∆𝜔𝑡 − 𝜑) − cos(2𝜔𝑡 + 𝜃𝐵 + 𝜔𝑡 + ∆𝜔𝑡 + 𝜑)]
2
𝐶
+ [cos(3𝜔𝑡 + 𝜃𝐶 − 𝜔𝑡 − ∆𝜔𝑡 − 𝜑) − cos( 3𝜔𝑡 + 𝜃𝐶 + 𝜔𝑡 + ∆𝜔𝑡 + 𝜑)]
2
+ … … … ..
𝐴
𝑆𝑖𝑔 ∗ 𝑟 = [cos(𝜃𝐴 − 𝜑 − ∆𝜔𝑡) − cos(2𝑤𝑡 + ∆𝜔𝑡 + 𝜃𝐴 + 𝜑)]
2
𝐵
+ [cos(𝜔𝑡 − ∆𝜔𝑡 + 𝜃𝐵 − 𝜑) − cos(3𝜔𝑡 + ∆𝜔𝑡 + 𝜃𝐵 + 𝜑 )]
2
𝐶
+ [cos(2𝜔𝑡 − ∆𝜔𝑡 + 𝜃𝐶 − 𝜑) − cos (4𝜔𝑡 + ∆𝜔𝑡 + 𝜃𝐶 + 𝜑)] + … … …
2

After LPF we get

𝑨
Y = 𝑺𝒊𝒈 ∗ 𝒓 = 𝐜𝐨𝐬(𝜽𝑨 − 𝝋 − ∆𝝎𝒕)
𝟐

After LPF we are not getting a DC value here too.

Now moving towards the magnitude

𝑹 = √𝑿𝟐 + 𝒀𝟐

And theta by the formula

𝒀
𝜽 = 𝐭𝐚𝐧−𝟏
𝑿

This difference in frequency will not affect the magnitude but the angle will be affected.
3. Sampling Rate- Nyquist and Shannon Criteria
Analog Signals are stored into digital computer using equidistant discrete samples. These sample
rate depends on the frequency of the sampling. Higher the sampling frequency higher will be the
number of samples thus more information is there and reconstruction of the signal from points
would be more accurate.

So the relationship between sampling frequency and signal frequency are proposed by Harry
Nyquist and Claude Shannon known as Nyquist Shannon theorem or criteria.

The minimum sampling frequency of a signal that it will not distort its underlying information,
should be double the frequency of its highest frequency component.

If fS is the sampling frequency, then the critical frequency (or Nyquist limit) fN is defined as equal
to fS/2.

3.1 Effects of ignoring Nyquist Criteria


Any sinusoidal component of the signal of frequency f' higher than fN (e.g. f' = fN + Δf) is not
only lost, but it is reintroduced in the sampled signal by folding at frequency fN as an alias
sinusoidal component of frequency f' = fN - Δf. This effect is known as aliasing (alias: false name).

Following figures show the graphical demonstration of Sampling Frequency, Sampling Frequency
and Aliasing Component/Frequency that is introduced due to ignoring theorem:
Figure 7: When Signal freq is less than the Nyquist freq, shows no aliasing component in the signal

By keeping sampling frequency constant and increasing signal frequency, aliasing factor is being
introduced.

Figure 8: When Signal freq is more than the Nyquist freq, shows aliasing component in the signal
3.2 Using Arduino Uno to determine the effects of the Sampling Rate
Normally, when you design an Arduino sketch, the Arduino executes all of the commands included
in the loop() function in the sequence in which they are written; however, timing events in the
loop is tricky (). Some commands take longer to perform than others, while others rely on
conditional statements (if, while...), and some Arduino library functions (such as digitalWrite or
analogRead) are made up of multiple commands. Timer interruptions on the Arduino allow you
to pause the typical series of events in the loop() function at precisely timed intervals while you
run a different set of commands. When these commands are completed, the Arduino returns to its
previous position in the loop ().

Interrupts restricts the normal flow of the program and run the Service Routine associated to that
interrupt. The Uno has three timers called timer0, timer1, and timer2. Each of the timers has a
counter that is incremented on each tick of the timer's clock. CTC timer interrupts are triggered
when the counter reaches a specified value, stored in the compare match register. Once a timer
counter reaches this value it will clear (reset to zero) on the next tick of the timer's clock, then it
will continue to count up to the compare match value again. By choosing the compare match value
and setting the speed at which the timer increments the counter, you can control the frequency of
timer interrupts.

In many situations, you will find that setting the counter speed to 16MHz is too fast. Timer0 and
timer2 are 8 bit timers, meaning they can store a maximum counter value of 255. Timer1 is a 16
bit timer, meaning it can store a maximum counter value of 65535. Once a counter reaches its
maximum, it will tick back to zero (this is called overflow).

This means at 16MHz, even if we set the compare match register to the max counter value,
interrupts will occur every 256/16,000,000 seconds (~16us) for the 8 bit counters, and every
65,536/16,000,000 (~4 ms) seconds for the 16 bit counter. Clearly, this is not very useful if you
only want to interrupt once a second.

Instead you can control the speed of the timer counter incrementation by using something called a
Pre-scaler. A prescaler dictates the speed of your timer according the following equation:
compare match register = [ 16,000,000Hz/ (prescaler * desired interrupt frequency) ] – 1
Bit Setting for defining prescalar is given below in the following table

3.3 Arduino Code with Explanation described by comments given below


void setup() {
Serial.begin(9600);
interrupt_function();
}
void loop() {

void interrupt_function()
{
noInterrupts();
// Clearing all register we will use
TCNT1 = 0;
TCCR1A =0;
TCCR1B =0;

//Now i will be finding the count value to be placed in a register. I will


be using following Formula

//Value = [ {Clock of the board (16MHz for UNO)} / {Pre-scalar * 1/desired


time interval} ] - 1;

// I have prescalar of 1024 I want Interrupt after each 2s so my value is


31249
//I am using Timer1 which is 16 bit can count upto 0 to 65536
//now i will set Comparing Registers OCR(1)A and OCR(1)B will automatically
count value

OCR1A= 31249;

//now i will set Timer Control register TCCR(1)A for comparing mode
TCCR1A |= (1 << WGM21);

//now i will set Pre-scalar using Control register TCCR(1)B


//I am going to use 1024 as a pre-scAlar
TCCR1B |= (1 << CS20) | (1 << CS21) | (1 << CS22);

TIMSK1 |= (1 << OCIE1A);

interrupts();
}
ISR(TIMER1_COMP1_vect)
{
unsigned int x =analogRead(A1);
Serial.println(x);
}

Sinusoidal signal is applied on the Arduino analog pin A1; Arduino reads the pin based on the
interrupt frequency. And Results were shown using serial plotter. By keeping Sampling frequency
constant, signal frequency was varied and effects of Nyquist criteria is studied.

Reference http://195.134.76.37/applets/AppletNyquist/Appl_Nyquist2.html
4. Software Modeling
4.1 Introduction
Phase Sensitive Detectors are used to detect and measure very small AC signals - all the way down
to a few nano volts! Accurate measurements may be made even when the small signal is obscured
by noise sources many thousands of times larger. For the highest accuracy, the reference signal
must have a very low harmonic content. In other words, it must be a very pure sine wave since any
additional harmonic content will cause distortion at the output.

In this Chapter, we have completed all the software part of our project in MATLAB. First, we have
generated Sine and Cosine wave using LUT table. We weren’t getting accurate results because in
Look up Table, we don’t know the exact value of any unknown point in between two sample
points. To remove this error, we have used interpolation in our final code to increase the sampling
rate and we can have a greater likelihood of obtaining a valid estimate.

4.2 Modeling with Interpolation


4.2.1 Code
clc
clear all
n=0:1:1023;
LUT= sin(((2*pi)/1024)*n);
Freq=500;
fs=220000;
Ts=1/fs;
Step=(Freq*1024)/fs

% ************************************************************************

fc= 10;
tow=1/(2*pi*fc);
a=exp(-Ts./tow);

f=500; % frequency which we want to extract


f1=700;
f2=1125;
f3=1300;

Signal(1)=0;
Y(1)=Signal(1);
Signal(1)=0;
B(1)=Signal(1);

count_int =1;
count_float=0;
delta=0;

count_int1 =256;
count_float1=255;
delta1=0;
for k=2:30000

%**************************************************************************
%************** SINE ******************************************************
n0=count_int;
Yn=LUT(n0);
n1=count_int+1;
if n1>1024
n1=n1-1024;
end

Yn1=LUT(n1);
internal(k)=Yn+(Yn1 - Yn)*delta;

count_float=count_float+Step;
if count_float>1024
count_float=count_float-1024+1;
end

count_int=floor(count_float);
delta=count_float- count_int;

%**************************************************************************

% *************COSINE******************************************************
m0=count_int1;
Ym=LUT(m0);
m1=count_int1+1;
if m1>1024
m1=m1-1024;
end

Ym1=LUT(m1);
internalcos(k)=Ym+(Ym1 - Ym)*delta1;

count_float1=count_float1+Step;
if count_float1>1024
count_float1=count_float1-1024+1;
end

count_int1=floor(count_float1);
delta1=count_float1- count_int1;

%**************************************************************************

%**************************************************************************

Signal(k)=sin(2*pi*f*(k-1)*Ts)+sin(2*pi*f1*(k-1)*Ts)+sin(2*pi*f2*(k-
1)*Ts)+sin(2*pi*f3*(k-1)*Ts); % Original Sine Wave
Z(k)= Signal(k)*internal(k); % Multiplication of Sine with Original
Sine Wave
D(k)= Signal(k)*internalcos(k); % Multiplication of CoS with Original
Sine Wave
Time(k)=(k/Ts);

Y(k)= (1-a)*Z(k)+a*Y(k-1); % Sine Wave FIlter


B(k)= (1-a)*D(k)+a*B(k-1); % Cosine Wave Filter

R(k)=sqrt(Y(k)^2+B(k)^2); % Output Result


angle(k)=atan( Y(k)/B(k));
t3(k)=(k-1)*Ts;

end

%**************************************************************************

%**************************************************************************
figure (1) % plot Sine Wave
plot (internal);
figure(2) % fft of sine wave
L=10*length(internal);
N=2^nextpow2(L);
Y0= (fft(internal,N));
Y1=abs(Y0);
f=0:fs/N:(fs/2-fs/N);
plot(f,Y1(1:N/2));

%**************************************************************************

%**************************************************************************
figure(3) % plot cos Wave
plot (internalcos);
figure(4) % fft of sine wave
L=10*length(internalcos);
N=2^nextpow2(L);
Y0= (fft(internalcos,N));
Y1=abs(Y0);
f=0:fs/N:(fs/2-fs/N);
plot(f,Y1(1:N/2));

%**************************************************************************

%**************************************************************************
% Original Sine Wave
figure (5) % plot original Signal
plot (Signal);
figure(6) % plot fft of the signal
L=10*length(Signal);
N=2^nextpow2(L);
Y0= (fft(Signal,N));
Y1=abs(Y0);
f=0:fs/N:(fs/2-fs/N);
plot(f,Y1(1:N/2));

%**************************************************************************
%**************************************************************************
% Compare Sine and Cosine Wave
figure(7)
hold on
plot(t3,internalcos);
plot(t3,internal, 'r');
hold off

%**************************************************************************

%**************************************************************************
% Multiplication with Sine Wave
figure(8) % Plot of mutliplication of Sine with original wave and its
filtered form
plot(Time,Z);
hold on
plot(Time,Y,'r');

figure(9) % fft of Sine multiplication with original sine wave


L=10*length(Z);
N=2^nextpow2(L);
Y0= (fft(Z,N));
Y1=abs(Y0);
f=0:fs/N:(fs/2-fs/N);
plot(f,Y1(1:N/2));

figure(10) % fft of the filtered Sine wave


L=10*length(Y);
N=2^nextpow2(L);
Y0= (fft(Y,N));
Y1=abs(Y0);
f=0:fs/N:(fs/2-fs/N);
plot(f,Y1(1:N/2));
hold off
%**************************************************************************

%**************************************************************************

% Multiplication with Cos Wave


figure(11) % Plot of mutliplication of Cos with original wave and its
filtered form
plot(Time,D);
hold on
plot(Time,B,'r');

figure(12) % fft of Cos multiplication with original Sine wave


L3=10*length(D);
N3=2^nextpow2(L3);
Y4= (fft(D,N3));
Y5=abs(Y4);
f=0:fs/N3:(fs/2-fs/N3);
plot(f,Y5(1:N3/2));

figure(13) % fft of the filtered cos wave


L4=10*length(B);
N4=2^nextpow2(L4);
Y6= (fft(B,N4));
Y7=abs(Y6);
f=0:fs/N4:(fs/2-fs/N4);
plot(f,Y7(1:N4/2));
hold off

%**************************************************************************

%**************************************************************************
figure (14) % plot result
plot (R);
figure(15) % fft of output
L=10*length(R);
N=2^nextpow2(L);
Y0= (fft(R,N));
Y1=abs(Y0);
f=0:fs/N:(fs/2-fs/N);
plot(f,Y1(1:N/2));

%**************************************************************************

%**************************************************************************

Sine Wave:
FFT of Sine wave:

Cos wave:
FFT of Cos wave:

Original Signal:
FFT of Original Signal:

Comparision b/w Sine and Cosine:


Multiplication of Sine with Original Signal:

FFT:
Filtered Sine Wave:

Cosine Wave multiplied with Original Signal:


FFT:

Filtered Cos Wave:


Output:

FFT:
4.2.2 Data from Oscilloscope
First we have considered original Signal by adding sine waves of different angles and amplitudes
then extracted required sine wave but now we have taken data from Oscilloscope. Checking on
Sine waves of different frequencies and amplitudes. We can also take data from arduino and sound
card. Processing of signal from taking data from sound card is in process. We are working on it as
it is our additional task.

Only change in code was to import excel file in MATLAB. We have saved data in USB and then
make an excel file imported to MATLAB file. Remaining code will remain same as shown above
with outputs.

4.2.3 Code

Figure 9 File Generated from attaching USB to Oscilloscope


5. How the Filter Works
5.1 Brief Description
Every time you provide a new value (xn), the exponential filter updates a smoothed value (yn):
yn = (1-w) × xn + w× yn – 1
Here:

 yn is the output of the filter at a moment in time n


 xn is the new input value at a moment in time n
 yn – 1 is the previous output value of the filter
 w is the weighting factor

w = exp(-Ts./tow)
tow=1/(2*pi*fc)
The filter essentially slows down the response to rapid changes (such as noise) in the input signal.

Figure 10 Response by changing Step in input Signal


This plot shows how the response to a step change in the input signal is affected by the filter
weight, w.
High values of w favor new data over old data. The output responds quickly to changes in the
input but is not smoothed much.
Low values of w favor old data over new data. The filter output is heavily smoothed and responds
slowly to changes (noisy or not) in the input.

The filter slows the response to step changes (such as noise) in the input signal. The thick, red line
represents a step input signal. When w is small, the output signal takes many measurements to
reach the input signal. When w is large, the response quickly reaches the input signal.

5.1.1 Code
fs=2000;
Ts=1/fs;
fc=[0 10 20 30 40 50 60 70 80 90 100 500 1000] ;
for i=2:13
tow(i)=1/(2*pi*fc(i));
a(i)=exp(-Ts./tow(i));
end

f=500;
Signal(1)=0;
Y(1)=Signal(1);
for i=2:13

for k=2:30
Signal(k)=heaviside(f*k);
Time(k)=(k/Ts);

Y(k)= (1-a(i))*Signal(k)+a(i)*Y(k-1);
end
figure(1)
plot(Time,Y);
hold on
plot(Time,Signal);
end

hence with the increase in cutoff frequency, the filter response time improves and it processes the
data faster.
6. Appendix
6.1 Comparison between Look-Up Table and Interpolation
6.2 Pure SINE and COSINE Wave
clc
clear all

Freq=10000; % frequency which we want to extract


fs=220000;
Ts=1/fs;
% ************************************************************************
time(1)=0;

for k=0:200-1
time(k+1)=k*Ts;
signal(k+1)= sin(2*pi*Freq*k*Ts);
end

%**************************************************************************
figure (11) % plot Sine Wave
plot (time,signal);

%
figure(12) % fft of sine wave
L=length(signal);
N=L+1000;
Y0= (fft(signal,N));

Ymax=max(abs(Y0));
Y1=mag2db(abs(Y0)/Ymax);
f=0:fs/N:(fs/2-fs/N);
plot(f,Y1(1:N/2));

title('Single-Sided Amplitude Spectrum of Pure Sin(t) ')


xlabel('f (Hz)')
ylabel('dB')
xlim([0 (fs/2-fs/N)])
% ylim([-100 0])

W = blackman(L) ;
SinW=W'.*signal;
A=[SinW ];
N=length(A)+1000;

figure(13)
plot (A);

Y0= (fft(A,N));
Ymax=max(abs(Y0));
Y1=mag2db(abs(Y0)/Ymax);

f=0:fs/N:(fs/2-fs/N);
figure (14)
plot(f,Y1(1:N/2));
title('Single-Sided Amplitude Spectrum of Sin(t) using LUT & interpolation
Windowing ')
xlabel('f (Hz)')
ylabel('dB')
xlim([0 (fs/2-fs/N)])
ylim([-100 0])

6.3 SINE and COSINE using Look-Up Table


clc
clear all
n=0:1:1023;
LUT= sin(((2*pi)/1024)*n);

Freq=500; % frequency which we want to extract


fs=220000;
Ts=1/fs;
Step=(Freq*1024)/fs

% ************************************************************************

count_int =1;
count_float=1;
delta=0;

time(1)=0;
for k=0:500000-1
time(k+1)=k*Ts;

%************** SINE ******************************************************


n0=count_int ;
phase(k+1)=count_int;

if n0>1024
n0=n0-1024;
end

Sin(k+1)=LUT(n0);

count_float=count_float+Step;
if count_float>1024
count_float=count_float-1024+1;
end

count_int=floor(count_float);
error(k+1)= Sin(k+1)-sin(2*pi*Freq*k*Ts);
phase_error(k+1)=count_float-count_int;

end
%*************************************************************************

L=length(Sin);
N=L

figure (1) % plot Sine Wave


plot (time,Sin);
title('Sine Time Plot(Interpolation) ')

figure(2) % fft of sine wave


Y0= (fft(Sin,N));
Ymax=max(abs(Y0));
Y1=mag2db(abs(Y0)/Ymax);

f=0:fs/N:(fs/2-fs/N);
plot(f,Y1(1:N/2));
title('Sine Time Plot Spectrum(LUT) ')

xlabel('f (Hz)')
ylabel('dB')
xlim([0 (fs/2-fs/N)]);
ylim([-100 0]);

figure(3)
W = blackman(L) ;

SinW=W'.*Sin;
A=[SinW ];
N=length(A);
plot (time,SinW);
title('Sine Blakman Windowed Time Plot((LUT)) ')

figure (4)
Y0= (fft(SinW,N));
Ymax=max(abs(Y0));
Y1=mag2db(abs(Y0)/Ymax);

f=0:fs/N:(fs/2-fs/N);
plot(f,Y1(1:N/2));

title('Sine Blakman Windowed Spectrum Plot(LUT) ')

xlabel('f (Hz)')
ylabel('dB')
xlim([0 (fs/2-fs/N)])
ylim([-100 0])
figure (5)
plot(time,error)
title('Sine eror time Plot(LUT) ')

figure (6)
Y0= (fft(error,N));
Ymax=max(abs(Y0));
Y1=mag2db(abs(Y0)/Ymax);

f=0:fs/N:(fs/2-fs/N);
plot(f,Y1(1:N/2));
title('Sine eror spectrum (LUT) ')

6.4 SINE and COSINE using Interpolation


clc
clear all
n=0:1:1023;
LUT= sin(((2*pi)/1024)*n);
Freq=500 % frequency which we want to extract
fs=220000;
Ts=1/fs;
Step=(Freq*1024)/fs

% ************************************************************************
count_int =1;
count_float=1;
delta=0;
error(1)=0;

time(1)=0;
for k=0:500000-1
time(k+1)=k*Ts;

%************** SINE ******************************************************


n0=count_int;

Yn=LUT(n0);
n1= n0+1;
if n1>1024
n1=n1-1024;
end

Yn1=LUT(n1);
Sin(k+1)=Yn+(Yn1 - Yn)*delta;

count_float=count_float+Step;
if count_float>1024
count_float=count_float-1024+1;
end

count_int=floor(count_float);
delta=count_float- count_int;

error(k+1)=(sin(2*pi*Freq*k*Ts)-Sin(k+1));

end
%************************************************************************

L=length(Sin);
N=L

figure (1) % plot Sine Wave


plot (time,Sin);
title('Sine Time Plot(Interpolation) ')

figure(2) % fft of sine wave

Y0= (fft(Sin,N));
Ymax=max(abs(Y0));
Y1=mag2db(abs(Y0)/Ymax);

f=0:fs/N:(fs/2-fs/N);
plot(f,Y1(1:N/2));
title('Sine Time Plot Spectrum(Interpolation) ')

xlabel('f (Hz)')
ylabel('dB')
xlim([0 (fs/2-fs/N)]);
ylim([-100 0]);

figure(3)

W = blackman(L) ;

SinW=W'.*Sin;
A=[SinW ];
N=length(A);
plot (time,SinW);
title('Sine Blakman Windowed Time Plot(Interpolation) ')

figure (4)
Y0= (fft(SinW,N));
Ymax=max(abs(Y0));
Y1=mag2db(abs(Y0)/Ymax);

f=0:fs/N:(fs/2-fs/N);
plot(f,Y1(1:N/2));
title('Sine Blakman Windowed Spectrum Plot(Interpolation) ')

xlabel('f (Hz)')
ylabel('dB')
xlim([0 (fs/2-fs/N)])
ylim([-100 0])

figure (5)
plot(time,error)
title('Sine eror time Plot(Interpolation) ')

figure (6)
Y0= (fft(error,N));
Ymax=max(abs(Y0));
Y1=mag2db(abs(Y0)/Ymax);

f=0:fs/N:(fs/2-fs/N);
plot(f,Y1(1:N/2));
title('Sine eror spectrum (Interpolation) ')

6.5 Results

Figure 11 When we use Interpolation, we get filtered signal with noise less than 60 dB which can be ignored.
Figure 12 Signal produced using Look-Up Table has noise in it which can cause distortion

Figure 13 Pure Sine Wave generated through MATLAB Formula has no distortion and fine output after applying filter

You might also like