Sunday, October 17, 2010

Signal processing in sagemath

I was experimenting with some tools for signal processing. The first tools that come to mind are opensource tools scilab and octave. I spent a day looking at the tools, the UI. I figured I could do definite integrals quite easily. Having spent the day and not having the made the progress I would have liked to, I turned to sagemath.

My first delight was to learn about piecewise functions. I decided to play with a square wave of time period 20.

I started with a function that looked like

f1(x) = 1
f2(x) = 0
f = Piecewise([[(0,10),f1],[(10,20),f2]])

Plotting the function with plot(f) showed






Which is exactly what I wanted. Piecewise class also supports a number of very useful functions related to fourier series.

One particularly useful one is a plot fourier series partial sum. The function shows how as we add more frequencies the fourier approximation to the original wave gets better

I started with

f.plot_fourier_series_partial_sum(5,10,-20, 20)

f.plot_fourier_series_partial_sum(15,10,-20, 20) gave me



f.plot_fourier_series_partial_sum(150,10,-20, 20) gave me



This looks like a good approximation to the wave we started with and almost looks like the signals I got on my oscilloscope during my engineering days :)

The next important thing was to get the sine and cosine coefficients

print "sine terms"
for j in range(0,21):
    pretty_print(f.fourier_series_sine_coefficient(j, 10))
print "cosine terms"
for j in range(0,21):
    pretty_print(f.fourier_series_cosine_coefficient(j, 10))



sine terms
0
2/pi
0
2/3/pi
0
2/5/pi
0
2/7/pi
0
2/9/pi
0
2/11/pi
0
2/13/pi
0
2/15/pi
0
2/17/pi
0
2/19/pi
0
cosine terms
1
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
 
 
Overall, I had good day with sagemath. I need to experiment with some of the discrete functions. Keep tuned in, I'll try and keep you updated on how it goes.

3 comments:

Zaid Siddiqui said...

Hi Balbir,

I am Zaid from Packt Publishing. We have recently published a new book on SAGE.With this book you can Unlock the full potential of Sage for simplifying and automating mathematical computing. Since you have an interest in SAGE, I thought that a review from you about the book will help it greatly.

you can check out the book at http://www.packtpub.com/sage-beginners-guide/book

I was wondering if you would be interested in reviewing the book for us. If so, I would love to send across a free review copy.

Do let me know.

Thanks,

Balbir said...

I Zaid, I'd love to review the book. I am unable to find your email address to respond.

Friley said...
This comment has been removed by the author.

Ranking and Unranking permutations

I've been a big fan of Skiena's Algorithm Design Manual , I recently found my first edition of the book (although I own the third ed...