root/pya/morph.py

Revision 237, 3.4 KB (checked in by ttm, 4 years ago)

pitch shift adicionado ao pya

Line 
1# -*- coding: utf-8 -*- #
2
3import pywt as O, pylab as P, numpy as N, time as T, scikits.audiolab as A
4
5def norm(sig):
6        factor = N.amax(N.abs(sig))
7        return sig/factor
8
9ctime = T.time()
10noise = s1 = N.random.uniform(-1,1,10*44100)
11sinusoid = s2 = N.cos(N.linspace(0,10*700*2*N.pi,10*44100))
12
13wp1 = O.WaveletPacket(s1, 'db5', 'sym',5)
14wp2 = O.WaveletPacket(s2, 'db5', 'sym',5)
15
16# endereco da folha (path) e o vetor de coeficientes (data)
17coeffs1 = [(node.path, node.data) for node in wp1.get_leaf_nodes(decompose=True)]
18coeffs2 = [(node.path, node.data) for node in wp2.get_leaf_nodes(decompose=True)]
19
20wpr = O.WaveletPacket(None, 'db5', 'sym', 5)
21
22choice = 0 # leaf wavlets alternation
23if choice == 0:
24        for i in xrange( len(coeffs1) ):
25                if i%2==0:
26                        wpr[ coeffs1[i][0] ] = coeffs1[i][1]
27                else:
28                        wpr[ coeffs1[i][0] ] = coeffs2[i][1]
29        morphed = wpr.reconstruct()
30        morphed = norm( morphed )
31        name = "0morphed_LeafSound.wav"
32        A.wavwrite(morphed, name ,44100)
33        print "Leaf Alternation in file: ", name
34
35choice = 1
36if choice == 1:
37        for i in xrange( len(coeffs1)):
38                if i%2==0:
39                        cr=N.copy( coeffs1[i][1] )
40                        cr[::2]=coeffs2[i][1][::2]
41                        wpr[ coeffs1[i][0] ] = cr
42                else:
43                        cr = N.copy( coeffs2[i][1] )
44                        cr[::2] = coeffs1[i][1][::2]
45                        wpr[ coeffs1[i][0] ] = cr
46       
47        morphed = wpr.reconstruct()
48        morphed = norm(morphed)
49        name="1morphed_LeafCoeffSound.wav"
50        A.wavwrite(morphed, name, 44100)
51        print "Leaf and Coeff Alternation in file: ", name
52
53choice = 2
54if choice == 2:
55        for i in xrange( len(coeffs1) ):
56                cr = N.copy(coeffs1[i][1])
57                cr[::2] = coeffs2[i][1][::2]
58                wpr[ coeffs1[i][0] ] = cr
59       
60        morphed = wpr.reconstruct()
61        morphed = norm( morphed )
62        name = "2morphed_CoeffSound.wav"
63        A.wavwrite(morphed, name ,44100)
64        print "Coeff Alternation in file: ", name
65
66choice = 3
67if choice == 3:
68        for i in xrange( len(coeffs1) ):
69                cr = N.copy(coeffs1[i][1])
70                inds = N.arange(len(cr))
71                N.random.shuffle(inds)
72                inds = inds[::2]
73                print "indexes used for second sound ",  inds
74                cr[inds] = coeffs2[i][1][inds]
75                wpr[ coeffs1[i][0] ] = cr
76       
77        morphed = wpr.reconstruct()
78        morphed = norm( morphed )
79        name = "3morphed_HalfShuffCoeffSound.wav"
80        A.wavwrite(morphed, name ,44100)
81        print "Half shuff Coeff in file: ", name
82
83choice = 4
84if choice == 4:
85        for i in xrange( len(coeffs1) ):
86                cr = N.copy(coeffs1[i][1])
87                inds = N.arange( N.random.randint(len(cr)) )
88                N.random.shuffle(inds)
89                print "indexes used for second sound ", inds
90                cr[inds] = coeffs2[i][1][inds]
91                wpr[ coeffs1[i][0] ] = cr
92       
93        morphed = wpr.reconstruct()
94        morphed = norm( morphed )
95        name = "4morphed_RandCoeffSound.wav"
96        A.wavwrite(morphed, name ,44100)
97        print "Random Coeff in file: ", name
98
99choice = 5
100if choice == 5:
101        for i in xrange( len(coeffs1) ):
102                pesos=N.random.uniform(0,1,len(coeffs1[i][1]))
103                cr = pesos*coeffs1[i][1]+(1-pesos)*coeffs2[i][1]
104                wpr[ coeffs1[i][0] ] = cr
105       
106        morphed = wpr.reconstruct()
107        morphed = norm( morphed )
108        name = "5morphed_UniRandWeightCoeffSound.wav"
109        A.wavwrite(morphed, name ,44100)
110        print "Random Coeff in file: ", name
111
112choice = 6
113if choice == 6:
114        for i in xrange( len(coeffs1) ):
115                pesos=N.random.uniform(0.5,0.1,len(coeffs1[i][1]))
116                cr = pesos*coeffs1[i][1]+(1-pesos)*coeffs2[i][1]
117                wpr[ coeffs1[i][0] ] = cr
118       
119        morphed = wpr.reconstruct()
120        morphed = norm( morphed )
121        name = "5morphed_NormRandWeightCoeffSound.wav"
122        A.wavwrite(morphed, name ,44100)
123        print "Random Coeff in file: ", name
124
125print ¨time ellapsed: ¨, ctime = T.time()
Note: See TracBrowser for help on using the browser.