Python edit
General edit
Running .py files in the interpreter edit
>>> exec(open('url.py').read())
Printing stuff...
Reloading modules edit
>>> import imp
>>> imp.reload(url)
Printing stuff...
<module 'url' from '.\\url.py'>
Adding directory to path edit
>>> import sys
>>> sys.path.append('C:\Python\Code')
>>> from url import parser
Finding variables and modules edit
>>> 'url' in globals().keys()
True
Dictionaries edit
>>> a = dict(first=1, second=2, third=3)
>>> b = {'first':1, 'second':2, 'third':3}
>>> a == b
True
>>> a['first']
1
>>> b['second']
2
>>> 'third' in a
True
>>> b.values()
dict_values([1, 2, 3])
Lists edit
>>> list = [2, 3, 4, 5]
>>> list.append(1) # add to the end of list
>>> list
[2, 3, 4, 5, 1]
>>> list.sort() # sort in ascending order
>>> list
[1, 2, 3, 4, 5]
>>> list.insert(0, 6) # insert 6 at position 0
>>> list
[6, 1, 2, 3, 4, 5]
>>> list[:3] # slice the list as desired
[6, 1, 2]
>>> list[3:5]
[3, 4]
>>> list.remove(4) # remove the number 4 from the list
>>> list
[6, 1, 2, 3, 5]
>>> list.sort(reverse=True)
>>> list
[6, 5, 3, 2, 1]
Number conversion edit
>>> x=8
>>> y=bin(x) # bin() will convert to a string
>>> y
'0b1000'
>>> type(y)
<class 'str'>
>>> int(y,2) # cast to int with base 2
8
>>> import cmath
>>> z = 5-3j
>>> type(z)
<class 'complex'>
>>> r, phi = cmath.polar(z) # convert complex to polar form
>>> r, phi
(5.830951894845301, -0.5404195002705842)
>>> cmath.rect(r, phi) # convert polar form back to complex
(5-3.0000000000000004j)
For loops edit
>>> list = [1, 2, 3, 4, 5]
>>> for i in list:
... print(i)
...
1
2
3
4
5
>>> for i in range(len(list)):
... print(list[i])
...
1
2
3
4
5
Scipy and Numpy edit
Integrals edit
Consider the following integral:
>>> import numpy as np
>>> func = lambda x: 2*np.exp(3*x)
>>> integrate.quad(func, 0, 4)
(108502.52761266934, 2.4994456113000214e-07)
Consider the following integral:
>>> func = lambda t: np.exp(-abs(3*t))
>>> integrate.quad(func, -np.inf, np.inf)
(0.6666666666666493, 7.022965268546203e-09)
Matrix operations edit
# Multiplication and dot product of two vectors
>>> a = np.array([1,2,3])
>>> b = np.array([4,5,6])
>>> a*b
array([ 4, 10, 18])
>>> np.dot(a, b)
32
# Find the inverse of a 3x3 matrix
>>> matrix = np.mat([[1,2,3],[4,5,6],[7,8,9]])
>>> matrix[0,:]
matrix([[1, 2, 3]])
>>> matrix[:,2]
matrix([[3],
[6],
[9]])
>>> inv = linalg.inv(matrix)
>>> inv
matrix([[ -4.50359963e+15, 9.00719925e+15, -4.50359963e+15],
[ 9.00719925e+15, -1.80143985e+16, 9.00719925e+15],
[ -4.50359963e+15, 9.00719925e+15, -4.50359963e+15]])
# Find the eigenvalues and eigenvectors of a 2x2 matrix
>>> b = np.mat([[1,1],[2,0]])
>>> d, v = linalg.eig(b)
>>> d
array([ 2., -1.])
>>> v
matrix([[ 0.70710678, -0.4472136 ],
[ 0.70710678, 0.89442719]])
# Replace elements in a matrix
>>> f = np.ones((5,5))
>>> f
array([[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.],
[ 1., 1., 1., 1., 1.]])
>>> r = np.array([2, 2, 2, 2, 2])
>>> f[1,:] = r
>>> f[:,0] = r
>>> f
array([[ 2., 1., 1., 1., 1.],
[ 2., 2., 2., 2., 2.],
[ 2., 1., 1., 1., 1.],
[ 2., 1., 1., 1., 1.],
[ 2., 1., 1., 1., 1.]])
Writing WAV files edit
from scipy.io.wavfile import write
def write_wave(duration, rate, freq, filename):
period = rate / float(freq)
omega = 2*np.pi / period
t = np.arange(duration*rate, dtype=np.float)
y = sp.sin(omega*t)
# Normalize and convert to 16-bit integer
z = np.int16(y/np.max(np.abs(y)) * 32767)
sp.io.wavfile.write(filename, rate, z)
Math edit
Complex numbers edit
Rectangular form | Polar form | Exponential form |
---|---|---|
For polar form, can be in degrees or radians. For exponential form, must be in radians.
Complex to exponential edit
Example of transferring a complex representation to an exponential representation.
Exponential to polar and rectangular edit
Here we transfer an exponential back to polar and rectangular form.
Vectors edit
The dot product of two vectors results in a scalar. Two vectors are orthogonal if their dot product is zero.
The length of a vector is defined below.
A unit vector (which has a length of 1) can be normalized as shown below.
Matrices edit
Reduction row echelon form (RREF) edit
Leading '1' is the only non-zero entry in the column; rows of all zeroes are at the bottom for style purposes.
Inverse edit
General algorithm edit
Create an augmented matrix with the identity matrix, then put the augmented matrix in RREF form. The inverse will be represented on the right side of the augmented matrix.
Formula edit
Formula for inverse of a 3x3 matrix
Computing ranks and bases edit
The Gaussian elimination algorithm can be applied to any matrix . In this way, for example, some matrices can be transformed to a matrix that has a row echelon form like
where the *s are arbitrary entries and a, b, c, d, e are nonzero entries.
This echelon matrix contains a wealth of information about : the rank of is 5 since there are 5 non-zero rows in ; the vector space spanned by the columns of has a basis consisting of the first, third, fourth, seventh and ninth column of (the columns of a, b, c, d, e in ), and the *s tell you how the other columns of can be written as linear combinations of the basis columns. This is a consequence of the distributivity of the dot product in the expression of a linear map.
Eigenvectors and eigenvalues edit
Eigenvectors and eigenvalues are related to matrix transformations. Specifically, certain transformations result in scalar multiples of the original vector.
is the eigenvalue and is the eigenvector
is an eigenvalue of A if and only if the determinant of
Calculating eigenvectors edit
Suppose we have the following eigenvalue of a given vector, and we want to calculate the corresponding unit length eigenvector.
Now translate the matrix to RREF form, and remember that
If , then
Signal Processing edit
Polarization edit
Polarization refers to the orientation of the electric field during transmission. Remember the properties of electromagnetic wave, as illustrated below.
k is the direction of propagation, B is the magnetic field, E is the electric field
Additional Links edit
Phase Vocoder edit
Phase unwrapping edit
Multiples of 2 are discarded during the representation of a complex angle (based on the properties of the arctan function which will inherently "wrap" angles around the unit circle). If the phase were to be plotted, there would discontinuities (as shown below) when the phase crosses zero along the unit circle.
FFT frequency bins edit
Performing an FFT of 'N' samples will produce 'N' frequency bins, with an interval 'F/N' For real signal inputs, half of the frequency bins (from 'N/2 + 1' to 'N-1') are basically useless -- they are complex conjugates of the first half.
As an example, suppose we run the following code (with N = 1024 and F = 44100)...
>>> rate
44100
>>> len(frames[0])
1024
>>> fft = sp.fft(frames[0])
# Check to make sure we have 'N' bins
>>> len(fft)
1024
# The interval is 'F' / 'N'
>>> interval = rate / 1024
>>> abs(real(fft)).argmax()
12
>>> _ * interval
516.796875
# Can also be calculated using the fftfreq function
>>> freqs = sp.fftpack.fftfreq(frames[0].size, 1/rate)
>>> freqs[12]
516.796875
Phase code explained edit
We know the following facts:
- FFT of 'N' samples gives 'N' frequency bins
- Frequency interval between bins = rate/N
- Time for each sample = 1/rate
- Period = 1/F
- N/(1:N/2) is the length of the sinusoids at the center of bins 1:N/2 (remember that N/2 to N-1 are just complex conjugates)
- Therefore, bin 1 completes 1 cycle in N samples (N/1)
.......... bin 2 completes 1 cycle in N/2 samples (N/2)
.......... bin N/2 completes 1 cycle in 2 samples (N/(N/2))
We can show this with a bit of code:
# For bin 1
>>> rate = 44100
>>> fftSize = 1024
>>> time = 1/rate
>>> interval = rate/fftSize
>>> period = 1/interval
>>> period
0.023219954648526078
>>> time*fftSize
0.023219954648526078
# For bin 2
>>> rate = 44100
>>> fftSize = 1024
>>> time = 1/rate
>>> interval = rate/fftSize
>>> freq = interval*2
>>> period = 1/freq
>>> period
0.011609977324263039
>>> time*(fftSize/2)
0.011609977324263039
However, when calculating the phase shift, we must take into account the shift induced by our windowing:
- hopsize / (N/(1:N/2)) is the proportion of the cycle represented by the hop samples - 2*pi*hopsize / (N/(1:N/2)) is that proportion in radians - 2*pi*hopsize * ((1:N/2)/N) with cleaned up multiplication
Class notes edit
Eigenvectors of an LTI system (circulent matrix - finite length) are the complex harmonic sinusoids
Can stack the normalized eigenvectors into a matrix S that is NxN (complex) Can also create a frequency response matrix (F), which is a matrix of the corresponding eigenvalues along the diagonal
Where we wrote: y = Hx before, we can now write y = SFS(H)x