programming

EEG Signal Processing in Python and Scipy.Signal (1): Spectrum Estimation, FIR Filter Design, Convolution and Windowing

by Forrest Sheng Bao http://fsbao.net

I am doing a take-home midterm test of a class I am taking. So, I decided to use Python to to it. Below is a code for one problem. It includes several frequency used functions in classical signal spectral analysis and FIR filter design. The problem itself is to design bandpass filters over alpha to theta bands and apply them onto a EEG series, and plot the time domain and frequency domain signal, as well as the frequency response of filters. I used window methods to design FIR bandpass filters. The filters coefficients are smoothed by a Kaiser window. Filtering is implemented by convolving original signal with coefficients of filters.

summation and factorial in gprolog

by Forrest Sheng Bao http://fsbao.net

I can't run sicstus Prolog on my department Solaris workstation. That makes me very unhappy at 3 am. So I decide to use gprolog. And the programming can be easier. In gprolog, FD is built-in.

Factorial in gprolog:

factorial(0,1). 

factorial(N,F) :-  
   N>0, 
   N1 is N-1, 
   factorial(N1,F1), 
   F is N * F1.

Summation in gprolog

sum(0,0). 

sum(N,F) :-  
   N>0, 
   N1 is N-1, 
   sum(N1,F1), 
   F is N + F1.

It seems that "is" is also a built-in predicate in swi-prolog.

Butterworth, Chebyshev I/II and Elliptic filters in MATLAB

by Forrest Sheng Bao http://fsbao.net

% Copyleft (C) Forrest Sheng Bao 1984-2009 http://fsbao.net
% with Dept. of Electrical and Computer Engineering and Dept. of Computer Science, Texas Tech University, Lubbock, Texas, USA
%
% This program shows the orders and frequency/phase properties of four different kinds of filters, Butterworth, Chebyshev Type I, Chebyshev Type II and Elliptic.
% The filter is expected to have a cutoff frequency at 200 Hz with attenuation 40 db/octave. The sampling frequency is 2000 Hz.
%
% This program licensed in GNU GENERAL PUBLIC LICENSE (GPL) v3 or later.
% If you do not know what GNU GPL is, please visit http://www.gnu.org/copyleft/gpl.html

Welch method and autocorrelation of 1-D signals in MATLAB

by Forrest Sheng Bao http://fsbao.net

Here are some examples about computing autocorrelation, PSD in Welch methods with different windowing functions.

The problems are from John Semmlow's Biosignal and Medical Image Processing chapter 3. I used the MATLAB routine sig_noise() that comes with the textbook CD.

Problem 3.8 Welch method

Problem 3.9 Direct FFT spectrum vs FFT spectrum of a signal's autocorrelation function

Repeat above problem, but let SNR=0 dB

To say "Hello world" in Python CGI Web Programming in 5 minutes

by Forrest Sheng Bao http://fsbao.net

I have lotta Python programs for bioinformatics research. I wanted to put them onto the web. I only developed Web apps in PHP before. And it seemed to be a big pain for porting a Python program to the web. But, I figured out in 5 minutes.

First, you need an Apache server on your Linux/Mac OS/Windows box. If you are gonna use data base, you need a database server. There are tons of blogs addressing these issues. So I won't be gossipy here. Suppose everything we mention below happens on your server-even web browsing.

Video Tutorial: Debugging C++ programs in anjuta

by Forrest Sheng Bao http://fsbao.net

I just made a video tonight on how to debug a C++ program in anjuta, the open-source C/C++ IDE for Linux. anjuta is one component of GNOME development suite and thus part of GNOME project. I think it is very good, just a bit worse than Apple Xcode. But it is definitely way better than Microsoft Visual Studio. Microsoft should do some study on user interface design. Proper menu design, window layout and usability is more user-friendly than eye candies. Sadly, Microsoft focuses too much on eye candies, and makes using their software like dating a girl - every step is very complex.

domains, constraints and labeling in Sicstus Prolog solver, finite domain

by Forrest Sheng Bao http://fsbao.net

It's really tricky to program in Sicstus Prolog solver.

Bellow is an example

:- use_module(library(clpfd)).
a(X,Y):-domain([X], 1,4), domain([Y], 4, 6), X+2#>Y, labeling([],[X,Y]).

Results on Sicstus:

| ?- [d].
% consulting /home/grad2/fsbao/d.pl...
% consulted /home/grad2/fsbao/d.pl in module user, 10 msec 72 bytes
yes
| ?- a(X,Y).
X = 3,
Y = 4 ? ;
X = 4,
Y = 4 ? ;
X = 4,
Y = 5 ? ;
no

Ref: Constraint Logic Programming over Finite Domains, Sicstus Manual, http://www.sics.se/sicstus/docs/3.7.1/html/sicstus_33.html

No .h fot C++ Standard Library head file and g++ doesn't import the std namespace by default

by Forrest Sheng Bao http://fsbao.net

I want to talk about two common errors people encounter when they compile old programs, especially when they are using recent version of g++. They may wonder why this code can be compiled in g++3.4 but not g++4.3.

A lot people get shock when they see this:

error: fstream.h: No such file or directory

The answer to solve this problem is that "Header files in the C++ Standard Library do not use the .h extension; have have no extension," -Kyle Loudon, C++ Pocket Reference, p.5, O'Reilly, 2003. So instead of saying

#include <fstream.h>

, you should say

#include <fstream>

enumerate() array elements rather than indexing them in Python

by Forrest Sheng Bao http://fsbao.net

Recently, I am debugging two Python programs about graphs. I get very confused in many times, whether I am visiting an array element or the index of the array element. For example, I need to get the vertex number by indexing an array, and use that vertex number to index another array.

So, my friend Lay Yuan in Arizona State University told me a trick, using enumerate() in Python. This makes things much much more easier. I guess you are smart enough so this example can explain every thing to you

>>> for i,arr in enumerate([6,3,2,3,4]):
...     print i, arr
...
0 6
1 3
2 2
3 3
4 4

Python editing and debugging on Linux

by Forrest Sheng Bao http://fsbao.net

Python is my favorite programming language. There are many Python editors, debuggers and IDEs. Some good editors/IDE are geditor(comes with Gnome desktop by default), SPE (Stani's Python Editor) and Eric. There is a debugger with GUI called Winpdb. The default debugger is IDLE, developed by Python Software Foundation.

Syndicate content