Saturday, July 6, 2013

Coding the Confusing Circular Convolution and other DSP Woes

Image

I remember in my fifth  semester of Computer Science Engineering we had a subject called Digital Image Processing. It was initially a God darned subject. But, I soon began to understand the text about discrete signals and its manipulations. Unfortunately, the only way to survive the tests for this subject was to commit its various mathematical formulas for conversion and manipulation of signals to memory, a task I hate to date.
Maybe I believed in Einstein's idea (even before I came to know about it), as it is a logical deduction/conclusion every intelligent and creative soul must come to. Here's what Einstein said;


A link to a lifehacker blog post is attached to it, where you may go and read more on it. This is where the idea of Third Party APIs, extensible classes, refactoring, code reuse etc similar concepts came from. It is also the corner stone of Object Oriented Programming.

Most of you will agree to the fact that, "Programmers can be lazy." as said by Larry Wall. Hence, we reuse, refactor, inherit and incorporate others solutions in order to reduce the amount of effort required.

Now, returning to topic at hand. What I observed was that, most of my classmates were hard-coding such methodologies for each step of the computation, as taught in the theoretical classes, to solve such problems on paper. My progress was very slow in the first few lab sessions while most of my lab mates breezed past me. I was taken aback by my friend's approach trying to mimic each manual step into a hundred line code. I abhorred such menial labor irrespective of the fact that, you've "done the job" or not. It was an abuse to the community of scientific programming. Later I learned it violated the KISS (Keep it simple, stupid!) rule of modern programming.

I began seeking for a more streamlined and intuitive way to do such programs. In my search, I returned to the textbook and started coding the various formulas instead of each step we computed by hand (like in the age old days of the Intel 8085 and other obsolete microprocessors). For a first time programmer it may be a little confusing to produce an algorithmic form of the formula. This was a very good self-learning experience for me! I could run the textbook examples on my program and verify the solutions, too! It was the feeling of creating something working, elegant and new (at least for me) for the first time!
The added advantage of such an approach is that formulas, being flexible as they are, also made my functions flexible to handle any alteration in the questions they were to compute.

This crux of today's blog is - "Never hard-code! Never!".
 
I'm sure you'll see many, many coders "getting the job done" by some arcane code vomit on their editors but, in the end, it is not scalable. A true programmer always exploits the logic in a problem to come up with a flexible minimalist simple program while a true hacker exploits the logic in others programs to solder together a solution system that does more than the existing systems set of features/functions and solves the problem at hand.

One may hard code if his/her initial research (Google, books, StackOverflow etc aids) has returned a negative. He/She should also keep others informed of their approach and progress in order to attract outside review and improvements (An idea practiced in Pair Programming). Then find a general algorithm to improve such a solution. If the programmer succeeds in digitizing a solution for such a problem then he/she should give a pat on his/her back and share there achievements with others via a blog, open-sourcing or getting this algorithm published in a paper, if it's truly novel. After all, there is no intellectual achievement in re-inventing the wheel.

CIRCULAR CONVOLUTION CODIFIED -

Sorry for my previous meandering rant, if you're in a hurry. I just wanted you to know what you should know. Here's the Circular Convolution mathematical formula for discrete sequences with period N of functions h and x as:
Image

Observe the expanded mathematical statement uses two summations across elements of the two sets, thus we require to run two loops: one nested in another, to access each element of h for calculation with each element of x limited to the number of elements in the sets.

MATLAB source code for circular convolution is here.

Note:-

Other added programs are for DIFFT (Discrete Inverse Fast Fourier Transform), Ladder Form Generation , Poles and Order Calculation & Jury's Stability Test . I apologize,  for a semi-hard coded program for Jury's stability test. It is a shared work with my smart friend Chitransh Saurabh. I seem to have lost a lot of my DSP source-codes from yore. If you've implemented a simpler code with the KISS principle in mind (some of which I'm sure is floating around in the web), then please do share it with me and others. Also, I apologize for the stark lack of comments to the code, as I was a beginner then.

OTHER IMPORTANT SOURCE CODES:

MATLAB source code for DIFFT (Discrete Inverse Fast Fourier Transform) is here.

MATLAB source code for Ladder Form Generation is here.

MATLAB source code for Poles and Order Calculation is here.

MATLAB source code for Jury's Stability Test is here.

[*I'll be thankful for your appreciation and thank yous if you drop it in my comments below. Also, I know somehow this blog may aid my juniors and successors at BIT Mesra and it's extension centers or any other college in India, with a quick code to copy-paste for their lab tests, when no one's looking at them looking on the web for answers. Well then good luck for the test.]

No comments:

Post a Comment