Electronics Lab Philippines

DSP 0.01: A tutorial

Offline 0b00000111

  • *****
  • 6201
  • 405
  • There is no delight in owning anything unshared.
Re: DSP 0.01: A tutorial
« Reply #100 on: December 22, 2009, 11:30:33 AM »
I am not very familiar with the guitar effects algo.
But you could just experiment like adding delays, modulation of signals and others.

yup sir zerowing.. one of my first goal is to create a flanger effect ;D

sound demo:
Lovetone Flanger Part 1

madami akong magagamit dito sa mga natutunan ko sa thread na ito :) :) thanks
E-Gizmo Mechatronix Central: www.e-gizmo.com

Tel #: (63)(2) 536-3378
Globe +63915-973-7691
Smart +63921-779-0748

Location Map

YM: julie.egizmo  aka Born2BeWired  ;D

Offline zer0w1ng

  • *****
  • 2190
  • 308
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: DSP 0.01: A tutorial
« Reply #101 on: March 05, 2010, 08:16:24 AM »
From a long break, I will continue with this DSP tutorial.

On the next lessons, we will shift to Python from Scilab.
Python is faster and easier to use than Scilab without the problems we had experienced from our previous examples.

It can be downloaded here:
http://www.python.org/download/

We will also be needing some support software for Python in order to have Matlab-like functions and data arrays.
They are Numpy, Scipy and Matplotlib and can be downloaded here:
http://sourceforge.net/projects/numpy/files/
http://sourceforge.net/projects/scipy/files/
http://sourceforge.net/projects/matplotlib/files/

To really follow and have hands-on experience with this tutorial, I suggest to download and install them for I will still give examples in Python from this point on.

--

Previously, we had discussed basic time domain signal manipulations.
The next lessons will be on Frequency Domain processing/analysis.
The Cebuano Geek

Offline zer0w1ng

  • *****
  • 2190
  • 308
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: DSP 0.01: A tutorial
« Reply #102 on: March 08, 2010, 09:29:50 AM »
Using Python for DSP

We will be using Python on our next lessons.
This is a short introduction so one could follow the things/codes discussed here.

Python is a general language. Developers extended it so it can be used for DSP as well.
It has been considered as MATLAB alternative and we will be using this from now on.

I will give some basics on this language.
The language is very similar to others.
It is object-oriented but one could just ignore or don't use its objects advantage.
The code "grouping" is done using TABs and not curly brackets such in C and PHP or begin-end in others.

--
Assignment

Values can be assigned directly using "=" such as:

   a = 1
   b = 2

Where variables "a" and "b" is assigned with a value of 1 and 2, respectively.

--
Arrays

Arrays are defined using the square brackets "[]" and comma "," to separate each elements.

   varray = [1,2,3,4,5]

Multidimensional arrays are defined as array of arrays such as:

   array2d = [ [1,2,3,], [5,6,7], [7,8,9] ]


--
Numpy

The above arrays are Python arrays and behaves differently as arrays in Matlab.
In order to be Matlab-like, Numpy or Numerical Python is created.

Example, in using normal Python arrays:
Code: [Select]
>>> a = [1,2,3,4]
>>> b = [5,6,7,8]
>>> print a,b
[1, 2, 3, 4] [5, 6, 7, 8]
>>> print a+b
[1, 2, 3, 4, 5, 6, 7, 8]
>>>

Using Numpy arrays:
Code: [Select]
>>> from numpy import *
>>> a=array([1,2,3,4])
>>> b=array([5,6,7,8])
>>> print a,b
[1 2 3 4] [5 6 7 8]
>>> print a+b
[ 6  8 10 12]

See the difference.  
Numpy arrays are Matlab-like and its adding each elements both arrays and not concatenating them.

In our DSP discussion, we will ALWAYS be using Numpy.
http://numpy.scipy.org/

---
I would suggest to read a few quick tutorials on Python to be familiar with it.
http://coolnamehere.com/geekery/python/pythontut.html
http://docs.python.org/tutorial/
The Cebuano Geek

Re: DSP 0.01: A tutorial
« Reply #103 on: March 09, 2010, 01:41:12 AM »
subscribing to this lesson.. keep it up sir zer0 ;)
never saturate..

Offline zer0w1ng

  • *****
  • 2190
  • 308
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: DSP 0.01: A tutorial
« Reply #104 on: March 09, 2010, 07:29:36 AM »
Discrete Fourier Transform

Fourier theorem stated that any waveform can be made from combining series of sine waves.
This deals with infinite repeating waves.

In real world, we only deal with discrete values and have limited duration signals.  
Mostly these inputs came from an ADC and sampled at fixed intervals.
Thus the Discrete Fourier Transform is derived from the orignal Fourier Transform and only deals with finite data.
DFT is the function in DSP to convert time-domain signals to its frequency-domain representation.
FFT (fast fourier transform) in the other hand is still DFT but using an alternate computation method that reduces the number of math operations and runs much faster.

I will discuss DFT first and FFT later.

If you remembered our lesson on CORRELATION, and the example to detect radar chirp, DFT is doing the same process.
It is just a multiple correlation for each frequency bin.
Thus, the resulting values corresponds to the signal level of each frequency composing the orignal signal.

The formula for DFT is:



In Python, it is done this way:
Code: [Select]
########################################
#DSP Tutorial
#DFT - Discrete Fourier Transform
#by: Regulus Berdin rberdin@gmail.com
########################################

from numpy import *

def my_dft(x):
N = len(x)

y = zeros(N) + 0j # y = [0,0,0....,0] N number of elements
k = arange(N) # k = [0,1,2,...,N-1]

for n in range(N):
#compute correlating factor
W = exp(-2j*pi*k*n/N)

#correlate with signal
y[n] = sum(x * W)

return y

The returned results from the DFT function will be the frequency spectrum where half of number of input samples correspond to half of the sampling frequency.
For example for a 64 samples at 8KHz, the 32nd output corresponds to 4KHz frequency bin and the 16th sample is the 2KHz bin.
The upper half of the results is just a mirrored image of the lower half.
The results also is a complex number that defines the amplitude and phase of the single sine wave component of the signal of this frequency.

---

Example, we would want to get the spectrum of 2 signals, 1Khz and 300 Hz.

Code: [Select]
########################################
#DSP Tutorial
#DFT - Discrete Fourier Transform
#by: Regulus Berdin rberdin@gmail.com
########################################

from numpy import *
from pylab import *

def my_dft(x):
N = len(x)

y = zeros(N) + 0j # y = [0,0,0....,0] N number of elements
k = arange(N) # k = [0,1,2,...,N-1]

for n in range(N):
#compute correlating factor
W = exp(-2j*pi*k*n/N)

#correlate with signal
y[n] = sum(x * W)

return y

#####

fs = 8000.0
N = 80
y = range(N)

t = arange(0,N/fs,1.0/fs)
f1 = 300
f2 = 1000

signal = 0.5*sin(2*pi*f1*t) + 0.25*sin(2*pi*f2*t)
spectrum = my_dft(signal)

#graph results
clf()

subplot(211)
plot(y, signal)

subplot(212)
plot(y, abs(spectrum))
show()

The graphed results are shown below:


The top graph shows the time-domain signals of the added 2 sine waves.

The lower portion is the resulting spectrum with half is mirrored.
If you have noticed, frequency bin 10 represents the 1KHz level while frequency bin 3 the 300Hz.
Frequency bin 40 is the Nyquist limit (sampling_freq/2) which is the mirror line of the frequency domain results.
Take note also of the levels of the two frequency bins.  They are proportional to the levels of each respective added sine waves.

---

If you have questions or need clarification on the Python code or the DFT process, please post it here and I will answer it as I can.
The Cebuano Geek

Offline 0b00000111

  • *****
  • 6201
  • 405
  • There is no delight in owning anything unshared.
Re: DSP 0.01: A tutorial
« Reply #105 on: March 09, 2010, 08:11:22 AM »
ganda point for tita reggie! di na "unsigned char" ang ganda points mo ;D :D
E-Gizmo Mechatronix Central: www.e-gizmo.com

Tel #: (63)(2) 536-3378
Globe +63915-973-7691
Smart +63921-779-0748

Location Map

YM: julie.egizmo  aka Born2BeWired  ;D

Offline 'yus

  • *****
  • 4255
  • 300
  • Gender: Male
  • hw -> fw -> sw
    • yus' projects
Re: DSP 0.01: A tutorial
« Reply #106 on: March 09, 2010, 12:48:37 PM »
ayos.. tuloy na ang tutorial.. :)

yung wala pang na-download na python and/or wala pa nung numpy, scipy, matplotlib, etc:
pwede nyong gamitin itong Portable Eric 4 Python IDE (v2)
halos meron na lahat dyan ng kelangan natin para sa tutorial ni sir zer0w1ng.. :)
tapos nasa 64MB lang yung RAR file..

sample screenshots:

http://www.mypixelspace.net/imgs/img01/aoF2FzOxiY.jpg
join  - Philippine Electronics and Robotics Enthusiasts Club - www.philrobotics.com

Offline zer0w1ng

  • *****
  • 2190
  • 308
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: DSP 0.01: A tutorial
« Reply #107 on: March 18, 2010, 03:39:36 PM »
SPECTRUM LEAKAGE

DFT uses non-continous and finite signal and can produce spectral leakage even for a pure sine wave signal.
This is mostly seen if the frequency is not exact divisible of the sampling frequency.

For example:
Code: [Select]
########################################
#DSP Tutorial
#by: Regulus Berdin rberdin@gmail.com
########################################

from pylab import *

def my_dft(x):
N = len(x)

y = zeros(N) + 0j # y = [0,0,0....,0] N number of elements
k = arange(N) # k = [0,1,2,...,N-1]

for n in range(N):
#compute correlating factor
W = exp(-2j*pi*k*n/N)

#correlate with signal
y[n] = sum(x * W)

return y

###

fs = 8000.0
f = 233
t = arange(0, 0.1, 1.0/fs)
N = len(t)

signal = sin(2*pi*f*t) #generate sine wave
spectrum = my_dft(signal)[:N/2] #get the spectrum

subplot(211)
plot(signal)

subplot(212)
plot(abs(spectrum))

show()



As seen, the bins 0 to 50 except at the freq bin has levels higher than 0 and decreasing until the fs bin.
Our signal is pure sinewave thus other bins except our signal frequency bin should have 0 levels.
These signal levels at bins near our signal frequency are spectrum leakages.

The leakage is mostly due to abrupt start or ending of our finite signal wave during sampling.
As seen on the graph, our sampling ended with the sinewave at 90 degrees.

---

I will discuss on my next post on how to minimize this spectrum error due to leakage.
The Cebuano Geek

Offline Google

  • *
  • 1377
  • 20
Re: DSP 0.01: A tutorial
« Reply #108 on: June 09, 2010, 05:24:53 PM »
suscribing ....
If you limit your choices only to what seems possible or reasonable, you disconnect yourself from what you truly want, and all that is left is compromise.

Offline voltaman_krc

  • *****
  • 176
  • 4
  • Gender: Male
  • aruuuuu!
Re: DSP 0.01: A tutorial
« Reply #109 on: June 11, 2010, 08:57:38 PM »
subscribing.

Thanks sir zero.

Offline zer0w1ng

  • *****
  • 2190
  • 308
  • Gender: Male
  • Enter any 11-digit prime number to continue...
    • The Cebuano Geek
Re: DSP 0.01: A tutorial
« Reply #110 on: June 12, 2010, 06:26:45 AM »
Sorry, I have not updated new topics and late to continue this tutorial.
Marami kasing trabaho ngayon at minsan wala ng time kahit breaks.

Not to worry, I will try to complete the tutorial as much as I can.
The Cebuano Geek

Offline cray23

  • **
  • 15
  • 0
Re: DSP 0.01: A tutorial
« Reply #111 on: August 11, 2010, 11:32:39 PM »
Sir Zero salamat sa tutorial ng DSP, very practical nya../

Re: DSP 0.01: A tutorial
« Reply #112 on: January 23, 2011, 08:32:09 PM »
pabookmark
God give us the grace to accept with serenity the things that cannot be changed, courage to change the things which should be changed and the wisdom to distinguish the one from the other [Reinhold Niebuhr]

Offline ΔЅịM

  • ***
  • 3111
  • 151
  • Gender: Male
  • "Live Curious"
Re: DSP 0.01: A tutorial
« Reply #113 on: March 30, 2011, 11:28:30 AM »
master zerowing, salamat sa tutorial.. +1 po.. ;D ;D ;D ;D

paano po ung conversion n2 sa SciLAB sir... this is from MatLAB...

Code: [Select]
P2=poly([-2 -5 -6])                   %store polynomial (s+2)(s+5)(s+6) as P2 and display
pause

medjo Python na po ginagamit ninyo ngayon sir.. pasenxa po...
Live Curious.

Offline Interlock()

  • *****
  • 215
  • 16
  • Gender: Male
  • Puppet.Xander
Re: DSP 0.01: A tutorial
« Reply #114 on: September 23, 2011, 10:55:48 AM »
ang galing ni master zerowing...
waiting for an update :D
+1 po
Everything Under CONTROL.
Just Master the Basics.
In every action there's always an equal reaction.
- Newton's 3rd Law of Motion

Re: DSP 0.01: A tutorial
« Reply #115 on: October 11, 2011, 03:33:01 PM »
subscribing din po...

+1
GOD knows HUDAS not pay!

Offline LTSG

  • *****
  • 193
  • 10
  • Gender: Male
  • ~two things in life: RIGHT and WRONG~
Re: DSP 0.01: A tutorial
« Reply #116 on: October 24, 2011, 09:54:07 PM »
more! more! more!! :D

subscribing din...

Offline e-tronix

  • *
  • 1037
  • 27
Re: DSP 0.01: A tutorial
« Reply #117 on: May 30, 2012, 09:12:58 PM »
buhayin

Offline >>>vhinz08<<<

  • ******
  • 394
  • 5
  • Gender: Male
  • The best way to predict your future is to invent!
Re: DSP 0.01: A tutorial
« Reply #118 on: November 28, 2013, 11:32:01 AM »
sana ituloy tutorial na 2, very helpful...

-thanks sir zero...
Aim high and hit the mark!