Thursday, December 22, 2016

Listening to the Giants, Once Again!

It's been a long day :)
We have been working on a long term project to minimize human-elephant conflict in Sri Lanka by applying various technologies from our expertise in computer science and embedded systems. One such application we are working on is building a smart electric fence which can notify the maintainers and people protected by the fence about breakages of the fence wire at different places. The second application is a system which can locate elephants in the wild based on the infrasonic waves (low frequency sounds) they emit. In order to do that, we have designed and built low-cost infrasonic detectors by ourselves in the lab and have performed various experiments.

Meeting in the village.
In order to perform evaluations of the applications we have built, we have visited different places outside the lab environment and performed experiments with real-world conditions. Once I went with my lab colleagues to visit a domesticated elephant in order to record its sound about which you can read here. As an extension to all these experiments, about a week ago, we visited Udawalawa wildlife park and Hambegamuwa village which is situated in the edge of the wildlife park. There were two important goals of this visit. The first was to meet the villagers of Hambegamuwa and have a meeting to get their help on building our own electric fence in their premises. The second was to visit Udawalawa park and perform elephant infrasonic localization experiments. The journey was planned to be 2 days long since we had a lot of works to do.

Phantom-4 flying over the electric fence.
(video footage from the drone camera is shown
at the end of this article.)

On the journey day early morning, me and Namal came to lab and loaded all the equipment to the van. Then we went to Dr. Deepani's home to pick her. She is an ecologist who has been working on elephant conservation in Udawalawa area for many years. She is the one who mediate our contacts with the villagers and wildlife department officials. After she joined with us to the journey, we directly went to Dr. Kasun's home in Ambalangoda to pick him and Chathura. Dr. Kasun's home offered us our breakfast since we came from Colombo early morning without stopping to have breakfast. Finally, we all got together and set sail to Udawalawa.

Rain and the darkness failed to stop our guys.
When we arrive to Hambegamuwa town, it was evening. We directly went to the small guest house which we had selected to stay. We left our bags there and directly went to the village in Hambegamuwa for the meeting with the villagers which is the first target of our journey. The village is located at the edge of the Udawalwa wildlife park. Therefore, they are continuously getting hit by crop raiding elephants. The people in this small village are growing crops in the surrounding area and they live inside small houses under the threat of roaming elephants. To protect their village, they have an electric fence surrounding an area of approximately 20 acres. Their fence just like all the other electric fences breaks down frequently mostly because of the elephants. When the fence is broken, the villagers have a hard time locating the breakage by walking along the fence wire.

Preparing infrasonic detectors for the experiment.
In this small village, we had a brief meeting with the people and we agreed to donate them an electric fence energizer in order to build a new electric fence for them. They will provide the labor force and other materials such as poles and wires to build the fence while we are providing the most expensive electric fence energizer. We received this energizer as a donation from the students who did Google Summer of Code (GSoC) with our SCoRe lab. As a community service, we are going to donate that energizer to the villagers. In return, we get the chance to use this fence as an experimental testbed to try our electric fence breakage detection system which we developed as a result of a research in the lab.

Deploying a pair of infrasonic detectors.
After the brief meeting, we went to the location of the fence around their village and tried the fence energizer we had bought for them. We noticed that the voltage of the pulses in their existing fence is significantly higher than the voltage of the pulses provided by our energizer. Their energizer is a locally built one while ours is a branded energizer from an international manufacturer according to the standards of electric fencing. Even the villagers accepted that their current energizer is too dangerous to elephants as it can kill an elephant due to the higher voltage instead of driving it away. Therefore, use of an energizer built up to the standards is necessary for them. There was a small rain fall by the time we were inspecting the fence which was a disturbance as we we were dealing with high-voltage. When the darkness came, it was challenging to test our breakage detection system but we kept working under the torch lights. When we return to the guest house in Hambegamuwa town, it was about 9.00pm. The dinner was served to us at the house of the owner of the guest house. His house was located next to the guest house and he lived there with his family. After the dinner, we all went to sleep as we all were tired.

Calibrating the angle of an infrasonic detector.
The next morning, we woke up to start another long day as our plan was to test our elephant infrasonic localization system in the field. We have built low-cost infrasonic microphones and a set of firmware running on an embedded system which can be used to locate elephants from long distances. We have performed experiments on them at different places but our objective of this journey was to use it for real wild elephants. The owner of the guest house offered our breakfast again at his house before we start the journey. Namal was so busy from the morning as he had to prepare the microphones properly for the experiments. He even worked while we were traveling in the van from the guest house to the Udawalawa wildlife park entrance area.

Giants have been here...
After arriving to the Udawalawa wildlife park area, we parked the van near a temple and set up our microphones in the vehicle park as a start. We stayed there recording data for few hours while Dr. Kasun and Dr. Deepani went to have a meeting with the Udawalawa wildlife park warden. After the meeting, he returned with the good news that we can carry our quad-copter to their place and fly it around to take some pictures. Therefore, Chathura went with them in the van since he is our quad-copter specialist and the pilot (read more about our drone adventures here). Meanwhile, we decided that recording infrasonic data from the vehicle park is not a very successful approach as it receives a lot of noise from the vehicles going in the nearby road. Therefore, we decided to carry our two pairs of infrasonic detectors into the jungle.

Our hiding place where we spent the whole evening.
In order to get closer to the Udawalawa reservoir, we hand to crawl under an electric fence which was difficult with the equipment we were carrying. After getting closer to the Udawalawa reservoir, we found a nice hiding place where we can perform the sound recordings. We placed our infrasonic detectors on the ground and calibrated their settings to capture data continuously. Then, we stayed there with a pair of binoculars to note the sightings of elephants and the location based on the visual observation. Our hope was to compare the results of the captured data using the infrasonic detectors against our visual observation. An old fellow who lived in the temple where we parked our van came with us to this place and helped us in various ways for a while and finally went back to the temple. From that point onwards, only Namal and me was left near the reservoir.

Elephants in the distance as seen from the binoculars.
When it was lunch time, Chathura brought two lunch packets and a water bottle to us and went back in the van to the place where they were flying the quad-copter. Namal and me spent the whole evening at this location near the reservoir and had lunch at about 3.30pm while watching elephant herds coming and going to the Udawalawa reservoir. When we finish our lunch there was no any elephant near the reservoir and therefore we both decided to walk to the water and come back immediately if we notice any danger. By the way, while the microphones are recording data, we had nothing else to do there other than watching elephants using the binoculars. It was about 5.30pm when our guys returned to the location we were after flying the drone. Since it was late and many people warned us that elephants can arrive to the place we were hiding after the darkness falls, we decided to grab our stuff and move out of the jungle. We crawled again under the electric fence, loaded our equipment to the van parked near the temple and started moving. It was closer to 6.00pm and the time was right for the Udawalawa elephant orphanage to feed baby elephants with milk. So, we quickly moved in to that place to see it. Finally, we started our journey back to Colombo.




It was about 1.30am when I returned to home in Colombo and was so tired. However, the hope that our recorded data must contain some proof that our elephant localization system works in the real field kept spinning in my mind.

~********~

Sunday, December 18, 2016

Usage of NMCLI command

nmcli is a useful commandline tool on linux based systems to deal with the Network Manager without using GUI. In this post, I'm leaving some of my short notes about using it for connecting to WiFi networks.

Quickly check whether we are connected to the network using any
available device

# nmcli device status

check all the currently available connections

# nmcli connection show

turn off and on wifi

# nmcli radio wifi off
# nmcli radio wifi on


rescan for the available wifi networks

# nmcli device wifi rescan

list all the available wifi networks

# nmcli device wifi list

connect to a specific wifi network identified by ESSID ('eduroam' in this example)

# nmcli connection up eduroam

delete an existing connection from the network manager

#nmcli connection delete 3Com

connect to a wifi network

# nmcli device wifi connect 3Com password '1234'

adding a new connection information

# nmcli connection add type wifi con-name Asanka-WiFi ifname wlp1s0 ssid eduroam

That's it!

Monday, December 5, 2016

Audio Recording from the Linux Shell

This just a quick note about something I tried and hope that this information will be useful to me someday. First of all, let's check the available recording devices on our machine using the following command.

arecord -l

I received an output like the following.


Using the details we received from the above command, we can now capture any audio device as follows. If you want to record multiple devices at the same time, you can do that using two terminal windows. 

Built-in stereo sound card
ffmpeg -f alsa -ac 2 -i hw:1,0 -t 30 ~/Desktop/card1-device0.wav

USB mono sound card

ffmpeg -f alsa -ac 1 -i hw:2,0 -t 30 ~/Desktop/card2-device0.wav

 That's it!

Saturday, November 26, 2016

Prof.Thiemo's Visit to SCoRe lab in 2016

It is not necessary to mention that Prof.Thiemo Voigt is a regular visitor here. He used to visit Sri Lanka and meet our SCoRe group once in every year. This time, he arrived Sri Lanka on Last Sunday night. Without taking a break, the next day morning which was a Monday, he visited UCSC and met us. On this first day, we had organized a journey to goto Galle, Sri Lanka in order to allow him to goto the beach and get socialized with us. Everybody of our SCoRe group visited there with him. We all went to Jungle Beach in Unawatuna and had a bath in the sea. Then we had lunch from a nearby restaurant. It was about 5.00pm in the evening when we return to Colombo. Therefore, we didn't schedule any other work for that day.

Namal demonstrating the electric fence.
The next day, Thiemo delivered a talk in UCSC and his topic was "A highly efficient, low-cost backscatter reader". There he described a new technology they have published recently about a backscatter communication. After his talk and the lunch, we dedicated the rest of the evening to show off the outcomes of project works in our lab. Our guys in the lab had done a lot of hard work to prepare the demonstrations. Every thing went smoothly on the demonstrations. We mainly demonstrated the components of the elephant project which includes smart electric fence, infrasonic localization system and elephant detection with drone footages. In addition to the guys in our lab, some 4th year students also joined with us on this. It was a great day with so much enthusiasm. Finally, few guys from the lab joined with Thiemo to go for a dinner to which I didn't join since I had some other works.

Discussion at the infrasonic localization demonstration.
The third day of Thiemo's stay in Sri Lanka was the last day. We had a meeting in the morning to discuss about the next moves we should take on our collaborative project. Most importantly, we discussed about the potential improvements we can make in the paper which got rejected recently from a conference. We went through the comments of the reviewers and noted various actions to make as a response. Finally, we selected another venue to submit the paper soon after making improvements. In the evening of that day, Thiemo delivered a lecture for the MSc students. A part of the lecture was same as the guest talk he delivered a day before however the second part was something new. I attended to this lecture too and learned some new things. After the lecture, he took a little more time to have a on-line meeting with some student before conclude the day. Some of our guys again joined with him to go for dinner after which he left for the airport.

Thursday, October 27, 2016

Emulating Networks using CORE Network Emulator

I've been using Mininet in practical sessions for undergraduate computer networking courses for a while. The tool has great features and provided the required capabilities for our course works. However, Python scrips required to model a network topology was a trouble at some points because some students who were not good at Python programming faced difficulties in understanding networking concepts using Mininet. Meanwhile, I had a requirement to setup an IPv6 based network in a recent demonstration and found that Mininet is not capable of building IPv6 based networks.

After searching for a while on other available options, I found another interesting network emulator called CORE network emulator. This tool provided me easy setup of different network topologies by visually dragging and dropping components on a canvas. Setting IP addresses for each host and router interface could be done by either filling information on a graphical window or by using command line. It seems the features of the emulator are very intuitive and I believe that a student can easily self-learn how to model a network and test different things he/she learned using the emulator.

On a Ubuntu GNU/Linux 16.04 LTS machine, I was able to install the emulator by using the apt repositories easily as follows.

sudo apt-get update
 
sudo apt-get install core-network

We can start the emulator from the command line by using the following command which will bring up the emulator window.

core-gui

There are nice tutorials and resources to learn about how to use this emulator in the web. I will list some of them in the references list below.

References:




Thursday, October 20, 2016

Running a simple C program with Linux kernel

HelloWorld program as the init process
In this post, I'm writing down the steps I followed to compile Linux kernel and run it with a simple C program I wrote. First of all, we need to get some packages. I tried this on a 64-bit Ubuntu 16.04 machine.

sudo apt-get update
sudo apt-get install build-essentials libncurses5-dev libncursesw5-dev
sudo apt-get install gcc-multilib
sudo apt-get install qemu

After installing the required packages, let's create a directory for our working environment and then continue the rest there.

mkdir mylinux
cd mylinux
vim init.c

Inside the newly created C source file, insert the following code which will print the text "Hello World!" once compiled and run.

#include <stdio.h>
 
void main() {
  printf("Hello World!\n");
  while(1);
}

Now, run the following commands to compile the program and create a ram disk which will be used by the Linux kernel later. The selection of the file name init.c is special here because that is the name of the first process created by Linux kernel after booting up.

gcc -static init.c -o init
echo init|cpio -o --format=newc > initramfs


It's time to get the source code of the Linux kernel. Download it into the directory we created previously and build it by following the commands shown below.

wget https://cdn.kernel.org/pub/linux/kernel/v4.x/linux-4.8.1.tar.xz
tar xvJf linux-4.8.1.tar.xz
cd linux-4.8.1/
make defconfig
make


The above operation will take a significant time since the Linux kernel is a huge source code base which need to be compiled one by one and brought together to build the executable monolithic image. After the build process completed, you should be able to find the compiled kernel image in the following location.

linux-4.8.1/arch/x86_64/boot/bzImage

Now, let's run our program with the Linux kernel image on QEMU emulator using the following command.

qemu-system-x86_64 -kernel linux-4.8.1/arch/x86_64/boot/bzImage -initrd initramfs -append "console=tty1"

You should be able to see that the Linux kernel boots up and then it runs our init program at the end. Our program prints the "Hello World!" message and then hangs inside the infinite loop.

Saturday, October 15, 2016

An Ariticle in APNIC Blog

Few days ago, I received a chance to write an article in the official APNIC blog. APNIC is the registry organization of IP addresses in the Asia and Pacific regions. Recently they held their APNIC-42 conference in Colombo, Sri Lanka and as a part of it, they help various workshops. Dr. Kasun de Zoysa from UCSC got an opportunity to participate as a resource person in a workshop and there he presented some of our recent research works to minimize human-elephant conflict in Sri Lanka using IoT technologies.

At the end of the workshop, Dr. Kasun invited his colleague of the workshop Mr. Jonathan Brewer to see our SCoRe lab and the things we are doing there. Finally, he introduced us to the editors of the APNIC blog and invited us to write an article about our research work. The result was the article I wrote to them. Actually, it is not the original content I wrote to them but an edited one by the experienced editors.

Wednesday, October 5, 2016

An Unexpected Visit to Kandy

Last weekend became a traveling weekend unexpectedly. There was a website launching event as a part of the Elders Day celebration in Polgolla, Sri Lanka. This website was created by Mr. Anura Kasthoori who works with us partially attached to our lab. Since he is currently taking rest at home after a road accident he faced more than a month ago, he could not attend to this event. Therefore, Dr. Kasun asked me to participate to the ceremony on behalf of Mr. Kasthoori. Initially I had a plan to go to my home town in Matara in this weekend but due to this unexpected ceremony, I had to change my plans. Therefore, I decided to goto Polgolla with my wife to make this journey more interesting.

Last Saturday early morning, we started the journey from a van provided by the organizers of the ceremony who are a part of the Social Welfare Ministry. It was about 4.30am when we started from the university and we reached Polgolla before 8.00am. Even though the original agenda was to start the ceremony at 8.30am, it an extra 30minutes when it gets started. There was several interesting events in the ceremony such as distributing prizes and awards to some senior citizens and different musical and dancing performances. Since I was nervous about the launching of the website, I tested my laptops Internet connection and checked everything time to time until the website launching time arrives. Finally it came around 12.00pm and the website was launched by the hand of a minister of the parliament who attended to the ceremony.

At the end of the ceremony, the organizers distributed lunch packets to the attendees. Me and my wife collected our meal and came out of the conference hall. Even though we were offered to get transport facility to come back to Colombo in the same van we came in the morning, we refused since we decided to stay in Kandy till the next day. So, we took a bus from Polgolla to Kandy and then from the Kandy town, we took a taxi to get to a small hotel. Even though the place we found was not very good, we didn't have any option since we couldn't select and reserve a good hotel before we leave Colombo. So, we decided to manage with the limited things we had.

In the evening we walked from the hotel to the Temple of the Tooth. The walk was a little bit tiresome from the hotel to the temple but the nice view of the lake made us relaxed. We had dinner at a nearby Pizza Hut because I always love to enjoy pizza. It was about 9.00pm when we finish our dinner and start walking back to the hotel. I thought there would be crowd on the road but surprisingly the road around the lake was almost empty and looked not safe. So, we rushed to the hotel without waiting to see anything around. We couldn't visit any nearby places other than the Temple of the Tooth due to the limitations of time.

The next day early morning, which was a Sunday, we checked-out from the hotel and took a train from the Kandy railway station at 6.30am to Colombo Fort. I think this train journey was the most interesting part of this unexpected visit to Kandy. The sceneries of the mountains while traveling in the train made me so glad that we came there. We arrived to Colombo Fort railway station around 9.45 am without delays. It was a great unexpected journey.

Thursday, September 29, 2016

Research at CERN by Dr. Rudiger

About two weeks ago, I attended to a guest talk in Physics department about the latest research works in CERN. The speaker was Dr. Rudiger Voss and he is a senior researcher there. CERN is the place where they have Large Hadron Collider (LHC). It is a very important place for advancement of science in the recent history because they study about particles in the quantum scale. Since I was so busy during last few weeks, I didn't have some time to spare for writing a note in the blog. Finally, here I did it today :)

Sunday, August 28, 2016

DSP1 - Statistical Basics for Studying DSP


I have been trying to self-learn the field of Digital Signal Processing (DSP) since the last year. However, it seems I lost the motivation at some points and got busy with my other works too. The key information source for my self-learning approach was the book The Scientist and Engineer's Guide to Digital Signal Processing by Steven W. Smith. There are two great things about this book. First, it avoids mathematical jargons as much as possible when explaining concepts unlike many other DSP books. Second, it is freely available either to read on-line or to download for reading off-line.

I kept reading this book and when I come across something hard to understand, I skipped it even though that is not a good way to deal with hard to understand things. Somewhere after chapter 10, I got lost and gave up with reading it. But it seems it's hard to live on this planet without DSP and I decided to start it all over again. This time, my approach is different. I decided to take a more practical approach where I will write some codes and try the concepts presented in the book to see and understand them by doing. Let's see how it goes.

In this post, I'm going to write down some of the most basic stuff I came across while reading through the book's first two chapters.

  • What is a Signal ?

This is the first and one of the most important terms we come across while learning DSP. A Signal is a parameter that varies with another parameter in a system. Take the temperature of a room as an example which varies over time. We will be measuring the temperature in degrees of Celsius and the time in seconds. We can consider the temperature measurement as the parameter which varies with the time parameter. So, this temperature variation is a Signal. Take a look at the following graph which illustrate this signal.
Fig-1: Variation of temperature over time (mythical data)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import matplotlib.pyplot as plt
import numpy as np
from random import gauss

# generating dummy temperature data over time
sample = 60
x = np.arange(sample)

temp = list()
for i in range(0,sample):
    temp.append(gauss(27,0.1)) # gauss(mean, std)
y = np.array(temp)

# plotting the signal waveform
#plt.title('Waveform of temparature signal')
plt.xlabel('Time (s)')
plt.ylabel('Temperature (C)')
plt.ylim(25, 29)
plt.grid(True)
plt.plot(x, y)
plt.show()

It is really important to note that it is not necessary for the second parameter to be time always. The second parameter can be anything. For example, let's modify the above scenario slightly. We will place an array of 10 temperature sensors across the room with a 1 meter distances from each adjacent sensor and then take a temperature measurement from each of them at a single instance of time. So, this time we get a variation of temperatures in the room not over time but over space. We can take the second parameter as the distance from one corner of the room. Take a look at the following graph which illustrates this. Still, this is a signal.
Fig-2: Variation of temperature over space (mythical data)


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import matplotlib.pyplot as plt
import numpy as np
from random import gauss

# generating dummy temperature data over space
sample = 10
x = np.arange(sample)

temp = list()
for i in range(0,sample):
    temp.append(gauss(27,0.2)) # gauss(mean, std)
y = np.array(temp)

# plotting the signal waveform
#plt.title('Waveform of temparature signal')
plt.xlabel('Distance (m)')
plt.ylabel('Temperature (C)')
plt.ylim(25, 29)
plt.grid(True)
plt.plot(x, y)
plt.show()


  • What is the Waveform of a signal ?

When we plot the variation of the first parameter against the second parameter of a signal, what we get is the Waveform of the signal. In the previous two scenarios, what we have drawn are Waveforms of the two signals. It shows the shape of our signal nicely. But, there are some other aspects of a signal to look at other than its Waveform. We will see them later.

  • Mean and Standard deviation of a signal

Now we know what signals are and how to represent their waveform in a graph. By looking at the source codes shown above, it is obvious that we can simply represent any signal in an array in computer memory. When we have a signal, we can look at their statistical properties and the most important two properties comes in to the stage are Mean and Standard deviation of the signal's data samples.

Mean (\( \mu \)) is simply the average value of the data points in a signal. Mathematically, it can be represented as follows where \( x \) is the array containing our signal.

$$\mu = \frac{1}{N} \sum_{i=0}^{N-1} x_i$$

Standard Deviation (\( \sigma \)) of a signal is a measure of how much the signal data points are deviated from the mean value of the signal. Mathematically, it can be represented as follows where \( x \) is the array containing our signal and \( \mu \) is the mean of the signal which we have calculated already.

$$\sigma^{2} = \frac{1}{N-1} \sum_{i=0}^{N-1} (x_i - \mu)^{2}$$

Following program reads a wave file and then calculate the mean and standard deviation of the signal. It is interesting to note that a wave file is in stereo format where we have two channels. Therefore, we select one channel to plot the waveform graph as shown in the code example.

Fig-3: Waveform of a sound from an Owl


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
from random import randint
from random import gauss
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile

# read a signal from a Wav file
fs, data = wavfile.read("owl.wav")
# fs=sample rate, data=signal
# data has a value range from -2^15 to (2^15)-1
# because the data type is int16

# extract data from channel 0 (our wav file is stereo which has two chanels)
data = data[:, 0]
# to extract data from channel 1
#data = data[:, 1]

# showing the properties of the signal
print 'data:', data
print 'mean:' ,np.mean(data)
print 'std:', np.std(data)
print 'SNR:', np.mean(data)/np.std(data)

# showing the waveform of the signal in channel 0
#plt.title("Waveform (channel 0)")
plt.xlabel("Samples")
plt.ylabel("Amplitude")
plt.grid(True)
plt.xlim(0,140000)
plt.plot(data)
plt.show()


  • What is Signal-to-Noise Ratio (SNR) of a signal ?

When we are looking at a signal coming from some source, the mean value represents the actual value which we need to observe. Meanwhile, the standard deviation represents the errors or the noise that deviate our observed values. Therefore, we can divide the mean by standard deviation to get a ratio which represents how good our signal is compared to the background noise. We call this ratio Signal-to-Noise ratio or SNR.

  • Probability vs Statistics

I like the following explanation given in Stack Exchange by John D. Cook. It illustrates the difference between probability and statistics in a nice way.

Let's say we have a jar of red and green jellybeans. "A probabilist starts by knowing the proportion of each and asks the probability of drawing a red jelly bean. A statistician infers the proportion of red jelly beans by sampling from the jar."

  • Histogram of a signal

Now we are going to look at another way of illustrating a signal graphically. Let's think about our temperature measurement experiment again. We have a temperature sensor along with a clock so that we can take readings of room temperature at equally spaced time intervals. We can draw the waveform of this signal as follow.
Fig-4: Waveform of a the temperature signal over time


We can see from the waveform graph that same temperature reading can be measured at different discrete time instances. If we count the number of instances each temperature value has occurred in our signal, we can plot it in a histogram. The x-axis will be temperature values while the y-axis will be the count or number of times that temperature value is available on the signal. We can draw this histogram as follows.

Fig-5: Histogram of the temperature signal

We can use the following python script to generate the above graphs.


import matplotlib.pyplot as plt
import numpy as np
from random import gauss

# generating dummy temperature data over time
sample = 60
x = np.arange(sample)

temp = list()
for i in range(0,sample):
    temp.append(gauss(27,0.5)) # gauss(mean, std)
y = np.array(temp)

# plotting the signal waveform
#plt.title('Waveform of temparature signal')
plt.xlabel('Time (s)')
plt.ylabel('Temperature (C)')
plt.ylim(25, 29)
plt.grid(True)
plt.plot(x, y)
plt.show()

# plotting the histogram
plt.hist(y, normed=True)
#plt.title("Histogram of temperature signal")
plt.xlabel("Temperature (C)")
plt.ylabel("Frequency")
plt.grid(True)
plt.show()


  • Probability Density Function (PDF) for a signal

For a continuous signal, probability density function (PDF) has the x-axis with some parameter and the probability of getting that value in the y-axis. The graph shown below is the waveform for a signal. Let's say it was obtained by sampling the temperature in a room for a duration of 1 hour (3600 seconds). Probability density function for this signal has temperature in the x-axis and the probability of getting that temperature within the 1 hour period is given in the y-axis.
Fig-6: Waveform of a the temperature signal over 1 minute duration


This is what we get if we generate the probability density function for the signal we considered. In this case, we have received a Gaussian distribution shaped graph since our signal contains data which has a Gaussian distribution.
Fig-7: Probability density function of a the temperature signal


import matplotlib.pyplot as plt
import numpy as np
from random import gauss
from scipy.interpolate import UnivariateSpline

# generating dummy temperature data over time
sample = 3600
x = np.arange(sample)

temp = list()
for i in range(0,sample):
    temp.append(gauss(27,0.5)) # gauss(mean, std)
y = np.array(temp)

# plotting the signal waveform
#plt.title('Waveform of temparature signal')
plt.xlabel('Time (s)')
plt.ylabel('Temperature (C)')
plt.ylim(25, 29)
plt.grid(True)
plt.plot(x, y)
plt.show()

# plotting the PDF
p, x = np.histogram(y, bins=sample)
x = x[:-1] + (x[1] - x[0])/2   # convert bin edges to centers
f = UnivariateSpline(x, p, s=sample)
plt.plot(x, f(x)/sample)
#plt.title("PDF of temperature signal")
plt.xlabel("Temperature (C)")
plt.ylabel("Probability")
plt.grid(True)
plt.show()


  • Gaussian (normal) Distribution

When we sample a random variable which we find in the natural world, most of the times they are normally distributed. That means its distribution is a Gaussian (Normal) distribution that has a bell shape. Shape of this bell is controlled by the mean and standard deviation of the signal we are considering. Following equation illustrates the probability density function for the Gaussian distribution according to Wikipedia.

$$f(x \mid \mu,\sigma^2) = \frac{1}{\sqrt{2 \sigma^2 \pi}} \rm{e}^{-\frac{(x-\mu)^2}{2\sigma^2}} $$

Here are some Gaussian distributions for different mean \( \mu \) and standard deviation \( \sigma \) values. Python scripts used to generate these distributions are given after that.

Fig-8: Gaussian distributions for different mean and standard deviations.


import matplotlib.pyplot as plt
import numpy as np
from random import gauss
from scipy.interpolate import UnivariateSpline

# generating dummy temperature data over time
sample = 100000

temp = list()
for i in range(0,sample):
    temp.append(gauss(0,1)) # gauss(mean, std)
y0 = np.array(temp)

temp = list()
for i in range(0,sample):
    temp.append(gauss(0,0.2)) # gauss(mean, std)
y1 = np.array(temp)

temp = list()
for i in range(0,sample):
    temp.append(gauss(2,0.5)) # gauss(mean, std)
y2 = np.array(temp)

# plotting the PDF
p, x = np.histogram(y0, bins=sample)
x = x[:-1] + (x[1] - x[0])/2   # convert bin edges to centers
f = UnivariateSpline(x, p, s=sample)
plt.plot(x, f(x)/sample)

# plotting the PDF
p, x = np.histogram(y1, bins=sample)
x = x[:-1] + (x[1] - x[0])/2   # convert bin edges to centers
f = UnivariateSpline(x, p, s=sample)
plt.plot(x, f(x)/sample)

# plotting the PDF
p, x = np.histogram(y2, bins=sample)
x = x[:-1] + (x[1] - x[0])/2   # convert bin edges to centers
f = UnivariateSpline(x, p, s=sample)
plt.plot(x, f(x)/sample)

#plt.title("PDF of temperature signal")
plt.xlabel("x")
plt.ylabel("y")
plt.grid(True)

plt.legend(['mean=0,std=1', 'mean=0,std=0.2', 'mean=2,std=0.5'], loc='upper left')
plt.show()


  • Cumulative Distribution Function (CDF)

The cumulative distribution function of a normal distribution is the sum of probabilities of the x-axis variable in normal distribution up to each x-axis variable. Following graph illustrates this concept where the y-axis values vary between 0 and 1 since the total probability is 1 for all the x-axis values.

Fig-9: CDF for a normal distribution.


from random import gauss
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile

# generating a gaussian random signal
x = list()
for i in range(0,10000):
    x.append(gauss(0,10)) # gauss(mean, std)
x = np.array(x)

# showing the CDF of a signal
X = np.sort(x)
F = np.array(range(len(x)))/float(len(x))
plt.plot(X, F)
#plt.title("CDF")
plt.xlabel("Value")
plt.ylabel("Cumulative Frequency")
plt.grid(True)
plt.show()


  • Precision vs Accuracy of a measurement

I had came across these terms frequently at different places but it was only after reading the DSPGuide book I understood the true difference between then in terms of taking a measurement. When we talk about Precision of a measurement, we are talking about maximum variation or the possible error that can occur in successive measurements. When considering Accuracy, we are talking about how close our measurement is to the actual value that we are trying to measure.

I'll write more when I proceed to the next chapters of the book. Until then, enjoy processing digital signals!!!