Monday, May 19, 2014

22. Phase Modulation


Phase Modulation is very similar to Frequency Modulation.




We will use a sine table created using GEN10. Also, we will use a normalized x, such that 0 points to the first table entry and 1 points to the last table entry. The middle entry corresponds to 0.5.




The first instrument string creates a 1 Hz ramp, which is stored in a phasing variable. It acts as an index, to a sine table so the sine wave is generated in the audio output. Note, we have to set the mode to 1 so it knows that x=0 refers to the first entry in the table, and x=1 refers to the last entry of the table. Note we can also get the cosine, if the offset is 0.25 and wrap-around mode is used, so numbers like 1.25 map back to 0.25.


# Cos1.py

from moduleCsound import *
add(startSyn,startOpt,stopOpt,startIns)
header(nch=2)

# *** Instrument Strings ***
SinStr="""
aphas phasor 1
aout table aphas, 1, 1
out aphas,aout
"""

CosStr="""
aphas phasor 1
aout table aphas, 1,1,0.25,1
out aphas,aout
"""

instrument('sin',1,SinStr)
instrument('cos',2,CosStr)

add(stopIns,startSco)

# *** Table ***
table('1. Sine wave',1,0,8192,10,1)

# *** Score ***
rem('Sine wave')
score(1,0,1)
rem('Cosine wave')
score(2,1,1)

add(stopSco,stopSyn)
writeRun('Tut22a')



For the first second, we get the sine wave. In the next second, we have the cosine wave.




This results in a similar sine and cosine wave. Now the phasor term is added a value of 0.25.


# Cos2.py

from moduleCsound import *
add(startSyn,startOpt,stopOpt,startIns)
header(nch=2)

# *** Instrument Strings ***
SinStr="""
aphas phasor 1
aout table aphas, 1, 1
out aphas,aout
"""

CosStr="""
aphas phasor 1
aphas = aphas + 0.25
aout table aphas, 1,1,0,1
out aphas,aout
"""

instrument('sin',1,SinStr)
instrument('cos',2,CosStr)

add(stopIns,startSco)

# *** Table ***
table('1. Sine wave',1,0,8192,10,1)

# *** Score ***
rem('Sine wave')
score(1,0,1)
rem('Cosine wave')
score(2,1,1)

add(stopSco,stopSyn)
writeRun('Tut22b')



You can see that the phasor for the cosine portion goes from 0.25 to 1.25. It is clipped off at 1 for the audio as the maximum amp has been defined as 1.




Now the phase input is doubled, so the output signal is twice the frequency. Note by having different kinds of phases, we can have almost any kind of output. In phase modulation, we have to use the table opcode but the input phase could be any function.


# Sin2.py

from moduleCsound import *
add(startSyn,startOpt,stopOpt,startIns)
header(nch=2)

# *** Instrument Strings ***
SinStr="""
aphas phasor 1
aout table aphas, 1, 1
out aphas,aout
"""

Sin2Str="""
aphas phasor 1
aphas = aphas*2
aout table aphas, 1,1,0,1
out aphas,aout
"""

instrument('sin',1,SinStr)
instrument('sin2',2,Sin2Str)

add(stopIns,startSco)

# *** Table ***
table('1. Sine wave',1,0,8192,10,1)

# *** Score ***
rem('Sine wave')
score(1,0,1)
rem('Cosine wave')
score(2,1,1)

add(stopSco,stopSyn)
writeRun('Tut22c')



This shows that we do indeed get a sine wave with twice the frequency. Note, the input phase is clipped off at 1.5 sec, in the wave output. Internally, it works as expected in generating the cosine.




This shows a more complicated example. Here we have an Instrument String where the table is reading a phase from a sine wave, leading to a FM-like output.


# SinSin.py

from moduleCsound import *
add(startSyn,startOpt,stopOpt,startIns)
header(nch=2)

# *** Instrument Strings ***
SinStr="""
aphas phasor 1
aout table aphas, 1, 1
out aphas,aout
"""

SinSinStr="""
ain oscil 0.5, 1, 1
aout table ain, 1,1,0,1
out ain,aout
"""

instrument('sin',1,SinStr)
instrument('sinsin',2,SinSinStr)

add(stopIns,startSco)

# *** Table ***
table('1. Sine wave',1,0,8192,10,1)

# *** Score ***
rem('Sine wave')
score(1,0,1)
rem('SinSin wave')
score(2,1,1)

add(stopSco,stopSyn)
writeRun('Tut22d')



This shows the wav output. With more complex interconnections even more varied signals result. In the next Tutorial, we will go over more complex examples of Phase Modulation.




You will find additional information at pythonaudio.blogspot.com, including the source code.



This is video of Tutorial 22.



1 comment:

About Me

I have used Python for the last 10+ years. I have a PhD in Electrical Engineering. I have taught Assembly Language programming of Intel-compatible chips as well as PC hardware interfacing. In my research, I have used Python to automate my calculations in physics and chemistry. I also use C++ and Java, often with Python.