Haar Wavelet Exploration

116 days ago by andyasb

w = sqrt(5) 5^(.5) 
       
2.23606797749979
2.23606797749979
#I need to create a function of db4 #Maybe make this a class? def phi_db4(n): #The n is for 2^n iterations scale = 1.0/( 4*(2^(.5) ) ) h0 = scale*(1 + sqrt(3)) h1 = scale*(3 + sqrt(3)) h2 = scale*(3 - sqrt(3)) h3 = scale*(1 - sqrt(3)) 
       
#Transform a list representing a signal over [0,1] def Haar(s): count = 0 m = len(s) n = log(m, base = 2) ### Perform data checking: ### If 2d array, send to 2d program ### If entries are not 2^n, fill the rest in with zeros if not (m % 2 == 0): q = floor( log(m, base = 2) ) #fill in the rest t = [] #Blank initial list to work with as a copy of s. for u in range(m): t.append( s[u] ) #print "This should equal the input", t i = 1 j = 2 for l in [1..n]: m = m/2 #I should not have any problems using int division since assume m power of 2 for k in range(m): ### Compute and store :: I think this has to modify the s entry rather than ### The array t is a copy of the original a #count += 1 #count may be unnecessaryrray ### Initialize temp variables and then re-store #This is the a_k^(n-l) coefficient a = ( t[j*k] + t[(j*k)+i] ) / 2.0 #print a, "= (", str(t[j*k]),"+", str(t[j*k + i]), ")/2" #This the c_k^(n-l) coefficient b = (t[j*k] - t[(j*k)+i] ) /2.0 #print b, "= (", str(t[j*k]),"-", str(t[j*k + i]), ")/2" ### Re-store the values in the t array #This is the a_k^(n-l) coefficient t[j*k] = a #This is the c_k^(n-l) coefficient t[(j*k) + i] = b # This is problematic #print "t after k = ", str(k), "is ", t i = 2*i #count += 1 #count may be unnecessary j = 2*j return t #Inverse Transform a list representing a signal over [0,1] def inverseHaar(s): count = 0 y = len(s) n = int( log(y, base = 2) ) t = [] #Blank initial list to work with as a copy of s. for u in range(y): t.append( s[u] ) #print "This should equal the input", t i = 2^(n-1) j = 2 * i m = 1 u = [1..n] u.reverse() for l in u: for k in range(m): a = t[j*k] + t[(j*k) + i] #a_2k^(l-1) b = t[j*k] - t[(j*k) + i] #a_2k+1^(l-1) t[j*k] = a t[(j*k)+ i] = b j = i i = i/2 m = 2*m return t def wavelet_plot(s): #Populate a list, dividing [0,1] into as many segments as there are data points. u = [] n = len(s) ### at 0, at 1 so divide it into n-1 delta = 1.0/(n-1) for i in range(n-1): u.append(( (i*delta),(s[i])) ) u.append((1.0,s[n-1])) img = point(u, rgbcolor=hue(1), size=30).plot() img.show() ss = Haar([10,-2, 1, 3]) test = inverseHaar(ss) print test haar_plot(ss) #wavelet_plot(test) 
       
[10.0000000000000, -2.00000000000000, 1.00000000000000,
3.00000000000000]
Traceback (click to the left of this block for traceback)
...
NameError: name 'haar_plot' is not defined
[10.0000000000000, -2.00000000000000, 1.00000000000000, 3.00000000000000]
Traceback (most recent call last):        ### Perform data checking: 
  File "", line 1, in <module>
    
  File "/tmp/tmprxdDtq/___code___.py", line 87, in <module>
    haar_plot(ss)
NameError: name 'haar_plot' is not defined
g = (5,5,5,5,5, 7, 5, 5, 10, 2) w = Haar(g) w 
       
[5.25000000000000, 0.000000000000000, 0.000000000000000,
0.000000000000000, -0.250000000000000, -1.00000000000000,
0.500000000000000, 0.000000000000000, 6.00000000000000,
4.00000000000000]
[5.25000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, -0.250000000000000, -1.00000000000000, 0.500000000000000, 0.000000000000000, 6.00000000000000, 4.00000000000000]
r = [4,5,6] r.insert(0,1) ### at index zero, insert 1 r.insert(0,[1,2,3]) # I will have to collapse the list, add one at a time r 
       
[[1, 2, 3], 1, 4, 5, 6]
[[1, 2, 3], 1, 4, 5, 6]
s = [] t = [1,2,3,4] for d in t: s.append(d) t 
       
[1, 2, 3, 4]
[1, 2, 3, 4]
### This is the simple ordered Haar transform (every other, reorder) def orderedHaar(s): m = int( len(s) ) n = log(m, base = 2) h = [] # accumulator dynamic = [] for c in s: #### The first time I run the loop, dynamic = s dynamic.append(c) print dynamic #Should be same #### There are a total of n sweeps #### Maybe change this to while( len(dynamic) != 2) ? #### The for loop seems to satisfy the same condition for l in [1..n]: #maybe range would be better #print "" #print "" #print "At start of loop, dynamic =", dynamic ### I modify dynamic n times atemp = [] ctemp = [] dynsize = int( len(dynamic) ) # Perform operations on every two items in the sample for k in range(dynsize/2): atemp.append( ( dynamic[2*k] + dynamic[(2*k)+1] ) /2.0 ) ctemp.append( ( dynamic[2*k] - dynamic[(2*k)+1] ) /2.0 ) #### Append c to the accumulation in order #print atemp #print ctemp #### take ctemp, and append accumulate in order for z in h: ctemp.append(z) #print "Step one. This should be ctemp + h", ctemp #### restore that as accumulate h = [] for y in ctemp: h.append(y) #### At this point, ctemp is mutilated but it is recreated the next time it is referenced #### #print "The accumulated is ", h #### Re-store dynamic as a_temp dynamic = [] for j in atemp: dynamic.append(j) #### Append the final a which is the average over sample h.insert(0, atemp[0]) return h p = orderedHaar([3,1,0,4,8,6,9,9]) print "" print "" p 
       
[3, 1, 0, 4, 8, 6, 9, 9]


At start of loop, dynamic = [3, 1, 0, 4, 8, 6, 9, 9]
[2.00000000000000, 2.00000000000000, 7.00000000000000, 9.00000000000000]
[1.00000000000000, -2.00000000000000, 1.00000000000000,
0.000000000000000]
Step one. This should be ctemp + h [1.00000000000000, -2.00000000000000,
1.00000000000000, 0.000000000000000]
The accumulated is  [1.00000000000000, -2.00000000000000,
1.00000000000000, 0.000000000000000]


At start of loop, dynamic = [2.00000000000000, 2.00000000000000,
7.00000000000000, 9.00000000000000]
[2.00000000000000, 8.00000000000000]
[0.000000000000000, -1.00000000000000]
Step one. This should be ctemp + h [0.000000000000000,
-1.00000000000000, 1.00000000000000, -2.00000000000000,
1.00000000000000, 0.000000000000000]
The accumulated is  [0.000000000000000, -1.00000000000000,
1.00000000000000, -2.00000000000000, 1.00000000000000,
0.000000000000000]


At start of loop, dynamic = [2.00000000000000, 8.00000000000000]
[5.00000000000000]
[-3.00000000000000]
Step one. This should be ctemp + h [-3.00000000000000,
0.000000000000000, -1.00000000000000, 1.00000000000000,
-2.00000000000000, 1.00000000000000, 0.000000000000000]
The accumulated is  [-3.00000000000000, 0.000000000000000,
-1.00000000000000, 1.00000000000000, -2.00000000000000,
1.00000000000000, 0.000000000000000]


[5.00000000000000, -3.00000000000000, 0.000000000000000,
-1.00000000000000, 1.00000000000000, -2.00000000000000,
1.00000000000000, 0.000000000000000]
[3, 1, 0, 4, 8, 6, 9, 9]


At start of loop, dynamic = [3, 1, 0, 4, 8, 6, 9, 9]
[2.00000000000000, 2.00000000000000, 7.00000000000000, 9.00000000000000]
[1.00000000000000, -2.00000000000000, 1.00000000000000, 0.000000000000000]
Step one. This should be ctemp + h [1.00000000000000, -2.00000000000000, 1.00000000000000, 0.000000000000000]
The accumulated is  [1.00000000000000, -2.00000000000000, 1.00000000000000, 0.000000000000000]


At start of loop, dynamic = [2.00000000000000, 2.00000000000000, 7.00000000000000, 9.00000000000000]
[2.00000000000000, 8.00000000000000]
[0.000000000000000, -1.00000000000000]
Step one. This should be ctemp + h [0.000000000000000, -1.00000000000000, 1.00000000000000, -2.00000000000000, 1.00000000000000, 0.000000000000000]
The accumulated is  [0.000000000000000, -1.00000000000000, 1.00000000000000, -2.00000000000000, 1.00000000000000, 0.000000000000000]


At start of loop, dynamic = [2.00000000000000, 8.00000000000000]
[5.00000000000000]
[-3.00000000000000]
Step one. This should be ctemp + h [-3.00000000000000, 0.000000000000000, -1.00000000000000, 1.00000000000000, -2.00000000000000, 1.00000000000000, 0.000000000000000]
The accumulated is  [-3.00000000000000, 0.000000000000000, -1.00000000000000, 1.00000000000000, -2.00000000000000, 1.00000000000000, 0.000000000000000]


[5.00000000000000, -3.00000000000000, 0.000000000000000, -1.00000000000000, 1.00000000000000, -2.00000000000000, 1.00000000000000, 0.000000000000000]
#################################### ##### Wavelet Transforms ########### #################################### ### This is the simple ordered Haar transform (every other, reorder) def orderedHaar(s): m = int( len(s) ) n = log(m, base = 2) h = [] # accumulator dynamic = [] for c in s: #### The first time I run the loop, dynamic = s dynamic.append(c) #### There are a total of n sweeps #### Maybe change this to while( len(dynamic) != 2) ? #### The for loop seems to satisfy the same condition for l in [1..n]: #maybe range would be better ### I modify dynamic n times atemp = [] ctemp = [] dynsize = int( len(dynamic) ) # Perform operations on every two items in the sample for k in range(dynsize/2): atemp.append( ( dynamic[2*k] + dynamic[(2*k)+1] ) /2.0 ) ctemp.append( ( dynamic[2*k] - dynamic[(2*k)+1] ) /2.0 ) #### Append c to the accumulation in order #### take ctemp, and append accumulate in order for z in h: ctemp.append(z) #### restore that as accumulate h = [] for y in ctemp: h.append(y) #### At this point, ctemp is mutilated but it is recreated the next time it is referenced #### Re-store dynamic as a_temp dynamic = [] for j in atemp: dynamic.append(j) #### Append the final a which is the average over sample h.insert(0, atemp[0]) return h #Transform a list representing a signal over [0,1] def in_place_Haar(s): count = 0 m = len(s) n = log(m, base = 2) ### Perform data checking: ### If 2d array, send to 2d program ### If entries are not 2^n, fill the rest in with zeros if not (m % 2 == 0): q = floor( log(m, base = 2) ) #fill in the rest t = [] #Blank initial list to work with as a copy of s. for u in range(m): t.append( s[u] ) #print "This should equal the input", t i = 1 j = 2 for l in [1..n]: m = m/2 #I should not have any problems using int division since assume m power of 2 for k in range(m): ### Compute and store :: I think this has to modify the s entry rather than ### The array t is a copy of the original array ### Initialize temp variables and then re-store #This is the a_k^(n-l) coefficient a = ( t[j*k] + t[(j*k)+i] ) / 2.0 #print a, "= (", str(t[j*k]),"+", str(t[j*k + i]), ")/2" #This the c_k^(n-l) coefficient b = (t[j*k] - t[(j*k)+i] ) /2.0 #print b, "= (", str(t[j*k]),"-", str(t[j*k + i]), ")/2" ### Re-store the values in the t array #This is the a_k^(n-l) coefficient t[j*k] = a #This is the c_k^(n-l) coefficient t[(j*k) + i] = b # This is problematic #print "t after k = ", str(k), "is ", t i = 2*i j = 2*j return t #Inverse Transform a list representing a signal over [0,1] def in_place_inverseHaar(s): count = 0 y = len(s) n = int( log(y, base = 2) ) t = [] #Blank initial list to work with as a copy of s. for u in range(y): t.append( s[u] ) #print "This should equal the input", t i = 2^(n-1) j = 2 * i m = 1 u = [1..n] u.reverse() for l in u: for k in range(m): a = t[j*k] + t[(j*k) + i] #a_2k^(l-1) b = t[j*k] - t[(j*k) + i] #a_2k+1^(l-1) t[j*k] = a t[(j*k)+ i] = b j = i i = i/2 m = 2*m return t #################################### ##### Basic Plotting Functions ##### #################################### ### Make a sample function #### (ie for any f, be able to create a list 2^n long of samples ##### This assumes a domain of (0, 1) so it is flawed def wavelet_plot(s): #Populate a list, dividing [0,1] into as many segments as there are data points. u = [] n = len(s) ### at 0, at 1 so divide it into n-1 delta = 1.0/(n-1) for i in range(n-1): u.append(( (i*delta),(s[i])) ) u.append((1.0,s[n-1])) img = point(u, rgbcolor=hue(1), size=30).plot() img.show() #Depicts a wavelet as a sum of Haar wavelets def haar_plot(s): n = len(s) #The number of Psi arrays is the base two log (with powers up to the log-1) m = int( log(n, base = 2) ) #Plot the average value of the sample a_0 = phi(s[0],0.0,1.0) q_0 = a_0.plot() print("Average of the sample") q_0.show() print print count = 0 current = 1 #range from 0 to 2^(n-1) ####Sum function zero(x) = 0 h = Piecewise([ [(0,1.0), zero] ] ) h += a_0 #Add the average value #For each power from 0 to m-1 ::From the identity 2^n = sum(2^n-1) + 1 #For m = 2, I want powers 0 and 1 (total of [-] and [-,-] for k in range(m): #For each power do two things: #Create a list, Psi_plot(list) #go over the next 2^(k) elements and plot those #Iteration indices front = 2^k back = (2^(k+1)) - 1 u = [] #For each power test = [] for d in [front..back]: #my min and max should change based on the powers test.append((d-1)) u.append(s[d]) #u.append(s[current]) #current += 1 #At the end this should be be the power print "For items at", test, " s was sampled", u #count += 1 #count may be unnecessary h += psi_plot(u) print print "The sum of all wavelets" final = h.plot() final.show() ################################################################### ####### Analyze a function on an interval in terms of Haar wavelets ################################################################### #Takes in function f, interval d, and n for 2^n number of points #Returns a list that can be transformed and plotted def sample_f(f,d,n): #Create a delta to step through f at 2^n points delta = (d[1] - d[0]) / ( 2^n ) #Sample and populate the list s s = [] for j in range(2^n): s.append( f(x = j*delta) ) # so that s[j] = f(x = j*delta) return s #Returns a plot or sum of functions #Maybe make it an object? #Produces plots, returns a sum of functions def psi_plot(u): n = len(u) delta = 1.0/(n) ## Each psi needs its own min and max count = 0 #### Blank plot to sum with zero(x) = 0 h = Piecewise([ [(0,1.0), zero] ] ) blank = h.plot() #### Finds the xmin and xmax of the Psi functions, stores that to minMax list minMax = [] for j in range(n): minMax.append( ( (count*delta),( (count+1)*delta) ) ) count += 1 #### Plot the Psi functions #### Make h the accumulator function for k in range(n): temp = psi( u[k] , minMax[k][0] , minMax[k][1] ) ### Plot the individual Psi ### Make this an option, else it gets cluttered #print "The graph of Psi for s" + str(k) #temp_plot = temp.plot() #temp_plot.show() h += temp ### This function accumulates and returns at the end #at this point minMax returns the min and max values for each Psi print #### Make this part an optional keyword?? print "The sum of the Psi functions at frequency order", str(n) r = h.plot() r.show() #### All the individual one's show in loop. This may be desired to show the effect at a certain frequency return h #################################### ##### Wavelet Basis Functions ##### #################################### def psi(mag,xmin,xmax): f1(x) = mag f2(x) = -1 * mag fzero(x) = 0 mid = (xmax+xmin)/2.0 ###cases #Both endpoints on domain ends if ((xmin == 0) and (xmax == 1.0) ): f = Piecewise([ [(xmin,mid),f1],[(mid,xmax),f2] ]) #Left endpoint is 0 and right endpoint not 1 elif ((xmin == 0) and (xmax != 1.0) ): f = Piecewise([ [(xmin,mid),f1],[(mid,xmax),f2], [(xmax,1), fzero] ]) #Right endpoint is 1, left endpoint not 0 elif ((xmin != 0) and (xmax == 1.0) ): f = Piecewise([ [(0,xmin),fzero],[(xmin,mid),f1],[(mid,xmax),f2] ]) #Neither endpoints on domain end else: f = Piecewise([ [(0,xmin),fzero],[(xmin,mid),f1],[(mid,xmax),f2], [(xmax,1), fzero] ]) f.extend_by_zero_to(xmin=0, xmax=1) #should allow to add together return f def phi(mag,xmin,xmax): f1(x) = mag f = Piecewise([ [(xmin,xmax),f1] ]) return f ##### Main ###### ## ss = Haar([10,-2, 1, 3]) ## test = inverseHaar(ss) #haar_plot([5, 1, 3, -2]) #wavelet_plot(test) #haar_plot( [3,6,1,-1] ) #p = orderedHaar([5,6,4,1,-2,3,1,5]) #p #haar_plot(p) 
       
## Includes domain def wavelet_plot(s,d): #Populate a list, dividing [ d[0],d[1] ] into as many segments as there are data points. u = [] n = len(s) ### at 0, at 1 so divide it into n-1 delta = ( d[1] - d[0] )/(n-1) for i in range(n-1): u.append(( (i*delta),(s[i])) ) u.append((d[1],s[n-1])) img = point(u, rgbcolor=hue(1), size=30).plot() img.show() q = sin(x)*x^(2) - x^(.5)*cos(x/3) - x + 10*cos(x/7) q.plot([0,2]) w = sample_f(q,[0,2],4) p = orderedHaar(w) p haar_plot(p) wavelet_plot(w,[0,2]) q.plot([0,2]) 
       
Average of the sample



For items at [0]   s was sampled [0.000976562500000000*sin(1/8) +
0.00390625000000000*sin(1/4) + 0.00878906250000000*sin(3/8) +
0.0156250000000000*sin(1/2) + 0.0244140625000000*sin(5/8) +
0.0351562500000000*sin(3/4) + 0.0478515625000000*sin(7/8) -
0.0625000000000000*sin(1) - 0.0791015625000000*sin(9/8) -
0.0976562500000000*sin(5/4) - 0.118164062500000*sin(11/8) -
0.140625000000000*sin(3/2) - 0.165039062500000*sin(13/8) -
0.191406250000000*sin(7/4) - 0.219726562500000*sin(15/8) +
0.625000000000000*cos(1/56) + 0.625000000000000*cos(1/28) -
0.0220970869120796*cos(1/24) + 0.625000000000000*cos(3/56) +
0.625000000000000*cos(1/14) - 0.0312500000000000*cos(1/12) +
0.625000000000000*cos(5/56) + 0.625000000000000*cos(3/28) +
0.586726722769013*cos(1/8) - 0.625000000000000*cos(1/7) -
0.625000000000000*cos(9/56) - 0.0441941738241592*cos(1/6) -
0.625000000000000*cos(5/28) - 0.625000000000000*cos(11/56) -
0.0494105884401309*cos(5/24) - 0.625000000000000*cos(3/14) -
0.625000000000000*cos(13/56) - 0.679126587736527*cos(1/4) -
0.625000000000000*cos(15/56) - 0.0584633966683428*cos(7/24) +
0.0625000000000000*cos(1/3) + 0.0662912607362388*cos(3/8) +
0.0698771242968684*cos(5/12) + 0.0732877462472411*cos(11/24) +
0.0765465544619743*cos(1/2) + 0.0796721798998873*cos(13/24) +
0.0826797284707685*cos(7/12) + 0.0855816496101822*cos(5/8) +
1.12500000000000]

The sum of the Psi functions at frequency order 1

For items at [1, 2]   s was sampled [0.00195312500000000*sin(1/8) +
0.00781250000000000*sin(1/4) + 0.0175781250000000*sin(3/8) -
0.0312500000000000*sin(1/2) - 0.0488281250000000*sin(5/8) -
0.0703125000000000*sin(3/4) - 0.0957031250000000*sin(7/8) +
1.25000000000000*cos(1/56) + 1.25000000000000*cos(1/28) -
0.0441941738241592*cos(1/24) + 1.25000000000000*cos(3/56) -
1.25000000000000*cos(1/14) - 0.0625000000000000*cos(1/12) -
1.25000000000000*cos(5/56) - 1.25000000000000*cos(3/28) -
1.32654655446197*cos(1/8) + 0.0883883476483184*cos(1/6) +
0.0988211768802619*cos(5/24) + 0.108253175473055*cos(1/4) +
0.116926793336686*cos(7/24) + 1.50000000000000, 0.125000000000000*sin(1)
+ 0.158203125000000*sin(9/8) + 0.195312500000000*sin(5/4) +
0.236328125000000*sin(11/8) - 0.281250000000000*sin(3/2) -
0.330078125000000*sin(13/8) - 0.382812500000000*sin(7/4) -
0.439453125000000*sin(15/8) + 1.25000000000000*cos(1/7) +
1.25000000000000*cos(9/56) + 1.25000000000000*cos(5/28) +
1.25000000000000*cos(11/56) - 1.25000000000000*cos(3/14) -
1.25000000000000*cos(13/56) - 1.25000000000000*cos(1/4) -
1.25000000000000*cos(15/56) - 0.125000000000000*cos(1/3) -
0.132582521472478*cos(3/8) - 0.139754248593737*cos(5/12) -
0.146575492494482*cos(11/24) + 0.153093108923949*cos(1/2) +
0.159344359799775*cos(13/24) + 0.165359456941537*cos(7/12) +
0.171163299220364*cos(5/8) + 0.250000000000000]

The sum of the Psi functions at frequency order 2

For items at [3, 4, 5, 6]   s was sampled [0.00390625000000000*sin(1/8)
- 0.0156250000000000*sin(1/4) - 0.0351562500000000*sin(3/8) +
2.50000000000000*cos(1/56) - 2.50000000000000*cos(1/28) -
0.0883883476483184*cos(1/24) - 2.50000000000000*cos(3/56) +
0.125000000000000*cos(1/12) + 0.153093108923949*cos(1/8) +
2.62500000000000, 0.0625000000000000*sin(1/2) +
0.0976562500000000*sin(5/8) - 0.140625000000000*sin(3/4) -
0.191406250000000*sin(7/8) + 2.50000000000000*cos(1/14) +
2.50000000000000*cos(5/56) - 2.50000000000000*cos(3/28) -
2.50000000000000*cos(1/8) - 0.176776695296637*cos(1/6) -
0.197642353760524*cos(5/24) + 0.216506350946110*cos(1/4) +
0.233853586673371*cos(7/24) + 0.125000000000000,
0.250000000000000*sin(1) + 0.316406250000000*sin(9/8) -
0.390625000000000*sin(5/4) - 0.472656250000000*sin(11/8) +
2.50000000000000*cos(1/7) + 2.50000000000000*cos(9/56) -
2.50000000000000*cos(5/28) - 2.50000000000000*cos(11/56) -
0.250000000000000*cos(1/3) - 0.265165042944955*cos(3/8) +
0.279508497187474*cos(5/12) + 0.293150984988964*cos(11/24) +
0.125000000000000, 0.562500000000000*sin(3/2) +
0.660156250000000*sin(13/8) - 0.765625000000000*sin(7/4) -
0.878906250000000*sin(15/8) + 2.50000000000000*cos(3/14) +
2.50000000000000*cos(13/56) - 2.50000000000000*cos(1/4) -
2.50000000000000*cos(15/56) - 0.306186217847897*cos(1/2) -
0.318688719599549*cos(13/24) + 0.330718913883074*cos(7/12) +
0.342326598440729*cos(5/8) + 0.125000000000000]

The sum of the Psi functions at frequency order 4

For items at [7, 8, 9, 10, 11, 12, 13, 14]   s was sampled
[-0.00781250000000000*sin(1/8) - 5.00000000000000*cos(1/56) +
0.176776695296637*cos(1/24) + 5.06250000000000,
0.0312500000000000*sin(1/4) - 0.0703125000000000*sin(3/8) +
5.00000000000000*cos(1/28) - 5.00000000000000*cos(3/56) -
0.250000000000000*cos(1/12) + 0.306186217847897*cos(1/8) +
0.0625000000000000, 0.125000000000000*sin(1/2) -
0.195312500000000*sin(5/8) + 5.00000000000000*cos(1/14) -
5.00000000000000*cos(5/56) - 0.353553390593274*cos(1/6) +
0.395284707521047*cos(5/24) + 0.0625000000000000,
0.281250000000000*sin(3/4) - 0.382812500000000*sin(7/8) +
5.00000000000000*cos(3/28) - 5.00000000000000*cos(1/8) -
0.433012701892219*cos(1/4) + 0.467707173346743*cos(7/24) +
0.0625000000000000, 0.500000000000000*sin(1) -
0.632812500000000*sin(9/8) + 5.00000000000000*cos(1/7) -
5.00000000000000*cos(9/56) - 0.500000000000000*cos(1/3) +
0.530330085889911*cos(3/8) + 0.0625000000000000,
0.781250000000000*sin(5/4) - 0.945312500000000*sin(11/8) +
5.00000000000000*cos(5/28) - 5.00000000000000*cos(11/56) -
0.559016994374947*cos(5/12) + 0.586301969977929*cos(11/24) +
0.0625000000000000, 1.12500000000000*sin(3/2) -
1.32031250000000*sin(13/8) + 5.00000000000000*cos(3/14) -
5.00000000000000*cos(13/56) - 0.612372435695794*cos(1/2) +
0.637377439199098*cos(13/24) + 0.0625000000000000,
1.53125000000000*sin(7/4) - 1.75781250000000*sin(15/8) +
5.00000000000000*cos(1/4) - 5.00000000000000*cos(15/56) -
0.661437827766148*cos(7/12) + 0.684653196881458*cos(5/8) +
0.0625000000000000]

The sum of the Psi functions at frequency order 8


The sum of all wavelets


Average of the sample



For items at [0]   s was sampled [0.000976562500000000*sin(1/8) + 0.00390625000000000*sin(1/4) + 0.00878906250000000*sin(3/8) + 0.0156250000000000*sin(1/2) + 0.0244140625000000*sin(5/8) + 0.0351562500000000*sin(3/4) + 0.0478515625000000*sin(7/8) - 0.0625000000000000*sin(1) - 0.0791015625000000*sin(9/8) - 0.0976562500000000*sin(5/4) - 0.118164062500000*sin(11/8) - 0.140625000000000*sin(3/2) - 0.165039062500000*sin(13/8) - 0.191406250000000*sin(7/4) - 0.219726562500000*sin(15/8) + 0.625000000000000*cos(1/56) + 0.625000000000000*cos(1/28) - 0.0220970869120796*cos(1/24) + 0.625000000000000*cos(3/56) + 0.625000000000000*cos(1/14) - 0.0312500000000000*cos(1/12) + 0.625000000000000*cos(5/56) + 0.625000000000000*cos(3/28) + 0.586726722769013*cos(1/8) - 0.625000000000000*cos(1/7) - 0.625000000000000*cos(9/56) - 0.0441941738241592*cos(1/6) - 0.625000000000000*cos(5/28) - 0.625000000000000*cos(11/56) - 0.0494105884401309*cos(5/24) - 0.625000000000000*cos(3/14) - 0.625000000000000*cos(13/56) - 0.679126587736527*cos(1/4) - 0.625000000000000*cos(15/56) - 0.0584633966683428*cos(7/24) + 0.0625000000000000*cos(1/3) + 0.0662912607362388*cos(3/8) + 0.0698771242968684*cos(5/12) + 0.0732877462472411*cos(11/24) + 0.0765465544619743*cos(1/2) + 0.0796721798998873*cos(13/24) + 0.0826797284707685*cos(7/12) + 0.0855816496101822*cos(5/8) + 1.12500000000000]

The sum of the Psi functions at frequency order 1

For items at [1, 2]   s was sampled [0.00195312500000000*sin(1/8) + 0.00781250000000000*sin(1/4) + 0.0175781250000000*sin(3/8) - 0.0312500000000000*sin(1/2) - 0.0488281250000000*sin(5/8) - 0.0703125000000000*sin(3/4) - 0.0957031250000000*sin(7/8) + 1.25000000000000*cos(1/56) + 1.25000000000000*cos(1/28) - 0.0441941738241592*cos(1/24) + 1.25000000000000*cos(3/56) - 1.25000000000000*cos(1/14) - 0.0625000000000000*cos(1/12) - 1.25000000000000*cos(5/56) - 1.25000000000000*cos(3/28) - 1.32654655446197*cos(1/8) + 0.0883883476483184*cos(1/6) + 0.0988211768802619*cos(5/24) + 0.108253175473055*cos(1/4) + 0.116926793336686*cos(7/24) + 1.50000000000000, 0.125000000000000*sin(1) + 0.158203125000000*sin(9/8) + 0.195312500000000*sin(5/4) + 0.236328125000000*sin(11/8) - 0.281250000000000*sin(3/2) - 0.330078125000000*sin(13/8) - 0.382812500000000*sin(7/4) - 0.439453125000000*sin(15/8) + 1.25000000000000*cos(1/7) + 1.25000000000000*cos(9/56) + 1.25000000000000*cos(5/28) + 1.25000000000000*cos(11/56) - 1.25000000000000*cos(3/14) - 1.25000000000000*cos(13/56) - 1.25000000000000*cos(1/4) - 1.25000000000000*cos(15/56) - 0.125000000000000*cos(1/3) - 0.132582521472478*cos(3/8) - 0.139754248593737*cos(5/12) - 0.146575492494482*cos(11/24) + 0.153093108923949*cos(1/2) + 0.159344359799775*cos(13/24) + 0.165359456941537*cos(7/12) + 0.171163299220364*cos(5/8) + 0.250000000000000]

The sum of the Psi functions at frequency order 2

For items at [3, 4, 5, 6]   s was sampled [0.00390625000000000*sin(1/8) - 0.0156250000000000*sin(1/4) - 0.0351562500000000*sin(3/8) + 2.50000000000000*cos(1/56) - 2.50000000000000*cos(1/28) - 0.0883883476483184*cos(1/24) - 2.50000000000000*cos(3/56) + 0.125000000000000*cos(1/12) + 0.153093108923949*cos(1/8) + 2.62500000000000, 0.0625000000000000*sin(1/2) + 0.0976562500000000*sin(5/8) - 0.140625000000000*sin(3/4) - 0.191406250000000*sin(7/8) + 2.50000000000000*cos(1/14) + 2.50000000000000*cos(5/56) - 2.50000000000000*cos(3/28) - 2.50000000000000*cos(1/8) - 0.176776695296637*cos(1/6) - 0.197642353760524*cos(5/24) + 0.216506350946110*cos(1/4) + 0.233853586673371*cos(7/24) + 0.125000000000000, 0.250000000000000*sin(1) + 0.316406250000000*sin(9/8) - 0.390625000000000*sin(5/4) - 0.472656250000000*sin(11/8) + 2.50000000000000*cos(1/7) + 2.50000000000000*cos(9/56) - 2.50000000000000*cos(5/28) - 2.50000000000000*cos(11/56) - 0.250000000000000*cos(1/3) - 0.265165042944955*cos(3/8) + 0.279508497187474*cos(5/12) + 0.293150984988964*cos(11/24) + 0.125000000000000, 0.562500000000000*sin(3/2) + 0.660156250000000*sin(13/8) - 0.765625000000000*sin(7/4) - 0.878906250000000*sin(15/8) + 2.50000000000000*cos(3/14) + 2.50000000000000*cos(13/56) - 2.50000000000000*cos(1/4) - 2.50000000000000*cos(15/56) - 0.306186217847897*cos(1/2) - 0.318688719599549*cos(13/24) + 0.330718913883074*cos(7/12) + 0.342326598440729*cos(5/8) + 0.125000000000000]

The sum of the Psi functions at frequency order 4

For items at [7, 8, 9, 10, 11, 12, 13, 14]   s was sampled [-0.00781250000000000*sin(1/8) - 5.00000000000000*cos(1/56) + 0.176776695296637*cos(1/24) + 5.06250000000000, 0.0312500000000000*sin(1/4) - 0.0703125000000000*sin(3/8) + 5.00000000000000*cos(1/28) - 5.00000000000000*cos(3/56) - 0.250000000000000*cos(1/12) + 0.306186217847897*cos(1/8) + 0.0625000000000000, 0.125000000000000*sin(1/2) - 0.195312500000000*sin(5/8) + 5.00000000000000*cos(1/14) - 5.00000000000000*cos(5/56) - 0.353553390593274*cos(1/6) + 0.395284707521047*cos(5/24) + 0.0625000000000000, 0.281250000000000*sin(3/4) - 0.382812500000000*sin(7/8) + 5.00000000000000*cos(3/28) - 5.00000000000000*cos(1/8) - 0.433012701892219*cos(1/4) + 0.467707173346743*cos(7/24) + 0.0625000000000000, 0.500000000000000*sin(1) - 0.632812500000000*sin(9/8) + 5.00000000000000*cos(1/7) - 5.00000000000000*cos(9/56) - 0.500000000000000*cos(1/3) + 0.530330085889911*cos(3/8) + 0.0625000000000000, 0.781250000000000*sin(5/4) - 0.945312500000000*sin(11/8) + 5.00000000000000*cos(5/28) - 5.00000000000000*cos(11/56) - 0.559016994374947*cos(5/12) + 0.586301969977929*cos(11/24) + 0.0625000000000000, 1.12500000000000*sin(3/2) - 1.32031250000000*sin(13/8) + 5.00000000000000*cos(3/14) - 5.00000000000000*cos(13/56) - 0.612372435695794*cos(1/2) + 0.637377439199098*cos(13/24) + 0.0625000000000000, 1.53125000000000*sin(7/4) - 1.75781250000000*sin(15/8) + 5.00000000000000*cos(1/4) - 5.00000000000000*cos(15/56) - 0.661437827766148*cos(7/12) + 0.684653196881458*cos(5/8) + 0.0625000000000000]

The sum of the Psi functions at frequency order 8


The sum of all wavelets


def psi_plot(u): n = len(u) delta = 1.0/(n) ## Each psi needs its own min and max count = 0 #Used to plot zero(x) = 0 h = Piecewise([ [(0,1.0), zero] ] ) blank = h.plot() minMax = [] for j in range(n): minMax.append( ( (count*delta),( (count+1)*delta) ) ) count += 1 #Plot the Psi functions ###Make h the accumulator function for k in range(n): temp = psi( u[k] , minMax[k][0] , minMax[k][1] ) h += temp ### Plot the individual Psi print "The graph of Psi " + str(k) temp_plot = temp.plot() ###make this plot over [0,1] temp_plot.show() #at this point minMax returns the min and max values for each Psi print print "The sum of the Psi functions" r = h.plot() r.show() def psi(mag,xmin,xmax): f1(x) = mag f2(x) = -1 * mag fzero(x) = 0 mid = (xmax+xmin)/2.0 ###cases #Both endpoints on domain ends if ((xmin == 0) and (xmax == 1.0) ): f = Piecewise([ [(xmin,mid),f1],[(mid,xmax),f2] ]) #Left endpoint is 0 and right endpoint not 1 elif ((xmin == 0) and (xmax != 1.0) ): f = Piecewise([ [(xmin,mid),f1],[(mid,xmax),f2], [(xmax,1), fzero] ]) #Right endpoint is 1, left endpoint not 0 elif ((xmin != 0) and (xmax == 1.0) ): f = Piecewise([ [(0,xmin),fzero],[(xmin,mid),f1],[(mid,xmax),f2] ]) #Neither endpoints on domain end else: f = Piecewise([ [(0,xmin),fzero],[(xmin,mid),f1],[(mid,xmax),f2], [(xmax,1), fzero] ]) f.extend_by_zero_to(xmin=0, xmax=1) #should allow to add together return f haar_plot( [3,6,1,-1] ) 
       
The graph of Psi 0

The graph of Psi 1

The graph of Psi 2

The graph of Psi 3


The sum of the Psi functions
The graph of Psi 0

The graph of Psi 1

The graph of Psi 2

The graph of Psi 3


The sum of the Psi functions
fzero(x) = 0 f1(x) = x f2(x) = -0.5*x f = Piecewise([ [(0.0,0.0),fzero],[(0.0,0.45),f1],[(0.45,0.76),f2], [(0.76,1.0), fzero] ]) ### It appears that I have an issue when I try to define a function on zero interval g = f.plot() g.show() 
       
Traceback (click to the left of this block for traceback)
...
ZeroDivisionError: float division
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_4.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("Znplcm8oeCkgPSAwCmYxKHgpID0geApmMih4KSA9IC0wLjUqeApmID0gUGllY2V3aXNlKFsgWygwLjAsMC4wKSxmemVyb10sWygwLjAsMC40NSksZjFdLFsoMC40NSwwLjc2KSxmMl0sIFsoMC43NiwxLjApLCBmemVyb10gXSkgICAKZyA9IGYucGxvdCgpCmcuc2hvdygp"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmphWWzHB/___code___.py", line 7, in <module>
    g = f.plot()
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/functions/piecewise.py", line 981, in plot
    return sum([plot(f, a, b, *args, **kwds) for (a,b),f in self.list()])
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/misc.py", line 283, in wrapper
    return func(*args, **kwds)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/misc.py", line 138, in wrapper
    return func(*args, **options)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/plot.py", line 2488, in plot
    G = funcs.plot(*args, **original_opts)
  File "expression.pyx", line 7137, in sage.symbolic.expression.Expression.plot (sage/symbolic/expression.cpp:28501)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/misc.py", line 283, in wrapper
    return func(*args, **kwds)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/misc.py", line 138, in wrapper
    return func(*args, **options)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/plot.py", line 2507, in plot
    G = _plot(funcs, (xmin, xmax), *args, **kwds)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/plot.py", line 2608, in _plot
    data = generate_plot_points(f, xrange, plot_points, adaptive_tolerance, adaptive_recursion, randomize)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/plot.py", line 3574, in generate_plot_points
    data = srange(*ranges[0], include_endpoint=True)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/misc/misc.py", line 964, in srange
    count = (end-start)/step
ZeroDivisionError: float division
(0 == 0.0) 
       
Traceback (click to the left of this block for traceback)
...
SyntaxError: invalid syntax
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_2.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("OTAgPT0gMC4wKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpyO_kSp/___code___.py", line 3
    _sage_const_90  == _sage_const_0p0 )
                                       ^
SyntaxError: invalid syntax
(4 != 5) and ( 4 < 10) 
       
True
True
def psi(mag,xmin,xmax): f1(x) = mag f2(x) = -1 * mag fzero(x) = 0 mid = (xmax+xmin)/2.0 ###cases #Both endpoints on domain ends if ((xmin == 0) and (xmax == 1.0) ): f = Piecewise([ [(xmin,mid),f1],[(mid,xmax),f2] ]) #Left endpoint is 0 and right endpoint not 1 elif ((xmin == 0) and (xmax != 1.0) ): f = Piecewise([ [(xmin,mid),f1],[(mid,xmax),f2], [(xmax,1), fzero] ]) #Right endpoint is 1, left endpoint not 0 elif ((xmin != 0) and (xmax == 1.0) ): f = Piecewise([ [(0,xmin),fzero],[(xmin,mid),f1],[(mid,xmax),f2] ]) #Neither endpoints on domain end else: f = Piecewise([ [(0,xmin),fzero],[(xmin,mid),f1],[(mid,xmax),f2], [(xmax,1), fzero] ]) f.extend_by_zero_to(xmin=0, xmax=1) #should allow to add together return f #r = psi(5, 0, 0.5) #s = psi(2, 0, 0.75) #r.extend_by_zero_to(xmin = 0, xmax = 1) #s.extend_by_zero_to(xmin = 0, xmax = 1) #t = r+s #r.extend_by_zero_to(xmin=0, xmax = 1) #q = t.plot() #q.show() #r s = psi( -2, 0.25, 0.75) u = s.plot() u.show() t = psi( 4, 0.10, 0.60) v = t.plot() v.show() sum = s + t comb = sum.plot() comb.show() #t = r+s #u = t.plot() #u.show() ###I may have to explicitly define my piecewise functions with psi to be 0 ###Interesting work with piecewise #a(x) = 0.5 * x #b(x) = -2*x #f = Piecewise([ [(0,0.5),a],[(0.5,1),b] ]) #g = Piecewise([ [(0,0.5),b],[(0.5,1),a] ]) #h = f.plot() #i = g.plot() #h.show() #i.show() #l = f+g #k = l.plot() #k.show() 
       




psi_plot([3,6,2,6]) 
       
Traceback (click to the left of this block for traceback)
...
NameError: name 'psi_plot' is not defined
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_2.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("cHNpX3Bsb3QoWzMsNiwyLDZdKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpPm84qA/___code___.py", line 3, in <module>
    exec compile(u'psi_plot([_sage_const_3 ,_sage_const_6 ,_sage_const_2 ,_sage_const_6 ])
  File "", line 1, in <module>
    
NameError: name 'psi_plot' is not defined
f = psi(3, 0.25, 0.75) #f(x) = 2*x j(x) = 0 h = Piecewise([ [(0,1), j] ] ) blank = h.plot() gr = f.plot() mixed = blank + gr mixed.show() ####This box shows that piecewise functions plot differently #f1(x) = 1 #f2(x) = 1-x #f3(x) = exp(x) #f4(x) = sin(2*x) #g = Piecewise([[(0,1),f1],[(1,2),f2],[(2,3),f3],[(3,10),f4]]) #h = g.plot() #You can't plot a subset of a piecewise plot #h.show() 
       
w = haar_plot([5,4,2,-3]) w 
       
Average of the sample



For items at [1]   s was sampled [4]

For items at [2, 3]   s was sampled [2, -3]

Average of the sample



For items at [1]   s was sampled [4]

For items at [2, 3]   s was sampled [2, -3]

r = psi(4, 2, 5) q = r.plot() q.show() 
       
w = [5,4,1,10,6,10, -4, 2] u = wavelet_plot(w) 
       
(3.0 % 2 == 0) 
       
False
False
#Transform a list representing a signal over [0,1] import numpy as np ### You are given a 2d numpy array s def numpy_2d_Haar(s): count = 0 ### Assume that you are m = len(s) n = log(m, base = 2) t = [] #Blank initial list to work with as a copy of s. for u in range(m): t.append( s[u] ) #print "This should equal the input", t i = 1 j = 2 for l in [1..n]: m = m/2 #I should not have any problems using int division since assume m power of 2 for k in range(m): ### Compute and store :: I think this has to modify the s entry rather than ### The array t is a copy of the original array ### Initialize temp variables and then re-store #This is the a_k^(n-l) coefficient a = ( t[j*k] + t[(j*k)+i] ) / 2.0 #print a, "= (", str(t[j*k]),"+", str(t[j*k + i]), ")/2" #This the c_k^(n-l) coefficient b = (t[j*k] - t[(j*k)+i] ) /2.0 #print b, "= (", str(t[j*k]),"-", str(t[j*k + i]), ")/2" ### Re-store the values in the t array #This is the a_k^(n-l) coefficient t[j*k] = a #This is the c_k^(n-l) coefficient t[(j*k) + i] = b # This is problematic #print "t after k = ", str(k), "is ", t i = 2*i j = 2*j return t 
       
import numpy as np def 2dHaar(s): #Check if matrix is square, if not, make it square with dimensions near some 2^n #Extract dim v = dim(s) # I think I actually want to work with numpy arrays rather than Sage Matrices #For each row, run the 1 d haar method//bad idea, must alternate columns and rows #These indices double i = 1 j = 2 k = h= m = 2^v n = v for l in [1..n]: m = m/2 for k in range(m) omega = [[1,1,1,1,],[1,1,-1,-1],[1, -1,1, -1],[1,-1,-1,1]]t g = (5, 1, 2, 8) w = Haar(g) w 
       
[5.25000000000000, 0.000000000000000, 0.000000000000000,
0.000000000000000, -0.250000000000000, -1.00000000000000,
0.500000000000000, 0.000000000000000]
[5.25000000000000, 0.000000000000000, 0.000000000000000, 0.000000000000000, -0.250000000000000, -1.00000000000000, 0.500000000000000, 0.000000000000000]
t = [ 0, 9 , 8 , 7 ] len(t) ### Think pf operation on object rather than method t[2] for h in [4..7]: print h print str(15), "Hey", range(4) 
       
4
5
6
7
15 Hey [0, 1, 2, 3]
4
5
6
7
15 Hey [0, 1, 2, 3]
w = [ [1,2,3,4],[5,6,7,11],[23,1,-4,2],[2,14,2,6] ] w[3] dim(w) 
       
Traceback (click to the left of this block for traceback)
...
SyntaxError: invalid syntax
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_12.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dyA9IDwgWzEsMiwzLDRdLFs1LDYsNywxMV0sWzIzLDEsLTQsMl0sWzIsMTQsMiw2XT4Kd1szXQpkaW0odyk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpf6IlNi/___code___.py", line 3
    w = < [_sage_const_1 ,_sage_const_2 ,_sage_const_3 ,_sage_const_4 ],[_sage_const_5 ,_sage_const_6 ,_sage_const_7 ,_sage_const_11 ],[_sage_const_23 ,_sage_const_1 ,-_sage_const_4 ,_sage_const_2 ],[_sage_const_2 ,_sage_const_14 ,_sage_const_2 ,_sage_const_6 ]>
        ^
SyntaxError: invalid syntax
#Transform a list representing a signal over [0,1] def Haar(s): m = len(s) n = log(m, base = 2) t = [] #Blank initial list to store stuff to. #for u in range(m): # t.append(0) ### t must be a copy of s, not a blank list! for u in range(m): t.append( s[u] ) print t i = 1 j = 2 for l in [1..n]: m = m/2 for k in range(m): ### Compute and store :: I think this has to modify the s entry rather than ### The blank array t has been created #This is the a_k^(n-l) coefficient t[j*k] = ( s[j*k] + s[(j*k)+1] ) / 2.0 #This is the c_k^(n-l) coefficient t[(j*k) + 1] = (s[j*k] + s[(j*k)+1] ) /2.0 i = j j = 2*j return t 
       
### This is the simple ordered Haar transform (every other, reorder) def orderedHaar(s): m = int( len(s) ) n = log(m, base = 2) h = [] # accumulator dynamic = [] for c in s: #### The first time I run the loop, dynamic = s dynamic.append(c) print dynamic #Should be same #### There are a total of n sweeps #### Maybe change this to while( len(dynamic) != 2) ? #### The for loop seems to satisfy the same condition for l in [1..n]: #maybe range would be better print "" print "" print "At start of loop, dynamic =", dynamic ### I modify dynamic n times atemp = [] ctemp = [] dynsize = int( len(dynamic) ) # Perform operations on every two items in the sample for k in range(dynsize/2): atemp.append( ( dynamic[2*k] + dynamic[(2*k)+1] ) /2.0 ) ctemp.append( ( dynamic[2*k] - dynamic[(2*k)+1] ) /2.0 ) #### Append c to the accumulation in order print atemp print ctemp #### take ctemp, and append accumulate in order #### restore that as accumulate for g in ctemp: h.insert(0,g) print "The accumulated is ", h #### Re-store dynamic as a_temp dynamic = [] for j in atemp: dynamic.append(j) #if (len(dynamic == 2) ) #### Append the final a which is the average over sample h.insert(0, atemp[0]) return h p = orderedHaar([5,7,4,1,-2,3,1,5]) print "" print "" p 
       
 
       
### This is the simple ordered Haar transform (every other, reorder) def ordered_Haar(s): m = int( len(s) ) n = log(m, base = 2) h = [] # accumulator dynamic = [] for c in s: #### The first time I run the loop, dynamic = s dynamic.append(c) #### There are a total of n sweeps #### Maybe change this to while( len(dynamic) != 2) ? #### The for loop seems to satisfy the same condition for l in [1..n]: #maybe range would be better ### I modify dynamic n times a_temp = [] c_temp = [] dyn_size = int( len(dynamic) ) # Perform operations on every two items in the sample for k in range(dyn_size/2): a_temp.append[ ( s[2*k] + s[(2*k)+1] ) /2 ] c_temp.append[ ( s[2*k] - s[(2*k)+1] ) /2 ] #### Append c to the accumulation in order for g in c_temp: h.append(g) #### Re-store dynamic as a_temp dynamic = [] for j in a_temp: dynamic.append(j) #if (len(dynamic == 2) ) #### Append the final a which is the average over sample h.append(a_temp[0]) return h 
       
var('t') f = cos(2*pi*t^2) r = plot(f,t) r.show() 
       
var('t') f = sin(2*pi*t^2) r = plot(f,t) r.show() 
       
var('t') f = sin(4*t) + cos(7*t) #r = plot(f,t,(-10,10)) #r.show() q = f(t=4) print str(q) w = f(t=(4 + (2*pi))) print str(w) if (q==w): print 'periodic!' else: print 'not periodic' #print (q==w) 
       
sin(16) + cos(28)
sin(8*pi + 16) + cos(14*pi + 28)
periodic!
sin(16) + cos(28)
sin(8*pi + 16) + cos(14*pi + 28)
periodic!
var('x') a = sin(x) b = sin(-x) f = Piecewise([ [(-1.0,0.0),a],[(0.0,1.0),b] ]) r = plot(f,x,(-5.0,5.0)) r.show() 
       
Traceback (click to the left of this block for traceback)
...
TypeError: float() argument must be a string or a number
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_12.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dmFyKCd4JykKYSA9IHNpbih4KQpiID0gc2luKC14KQpmID0gUGllY2V3aXNlKFsgWygtMS4wLDAuMCksYV0sWygwLjAsMS4wKSxiXSBdKQpyID0gcGxvdChmLHgsKC01LjAsNS4wKSkKci5zaG93KCk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpMDk54u/___code___.py", line 7, in <module>
    r = plot(f,x,(-_sage_const_5p0 ,_sage_const_5p0 ))
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/misc.py", line 283, in wrapper
    return func(*args, **kwds)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/misc.py", line 138, in wrapper
    return func(*args, **options)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/plot.py", line 2488, in plot
    G = funcs.plot(*args, **original_opts)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/functions/piecewise.py", line 981, in plot
    return sum([plot(f, a, b, *args, **kwds) for (a,b),f in self.list()])
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/misc.py", line 283, in wrapper
    return func(*args, **kwds)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/misc.py", line 138, in wrapper
    return func(*args, **options)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/plot.py", line 2488, in plot
    G = funcs.plot(*args, **original_opts)
  File "expression.pyx", line 7137, in sage.symbolic.expression.Expression.plot (sage/symbolic/expression.cpp:28501)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/misc.py", line 283, in wrapper
    return func(*args, **kwds)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/misc.py", line 138, in wrapper
    return func(*args, **options)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/plot.py", line 2514, in plot
    G = _plot(funcs, (var, xmin, xmax), *args, **kwds)
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/plot.py", line 2533, in _plot
    funcs, ranges = setup_for_eval_on_grid(funcs, [xrange], options['plot_points'])
  File "/home/sage/sage_install/sage-4.3.5/local/lib/python2.6/site-packages/sage/plot/misc.py", line 386, in setup_for_eval_on_grid
    ranges = [[float(z) for z in r] for r in ranges]
TypeError: float() argument must be a string or a number
a(x) = sin(x) b(x) = sin(-1*x) f = Piecewise([ [(-2*pi,0.0),b],[(0.0,2*pi),a] ]) #g = Piecewise([ [(0,0.5),b],[(0.5,1),a] ]) h = f.plot() #i = g.plot() h.show() #i.show()