Saturday, May 17, 2014

20. Amplitude Modulation


csound has many opcodes related to frequency modulation. To understand frequency modulation, you have to understand the amplitude modulation.




Amplitude Modulation is used in communications to send a signal over distances. The signal that we want to send has some maximum frequency, say f1. It will be modulated, that is, multiplied, by a frequency f2, acting as our carrier.




The result of multiplying two sines is the sum and difference of the original frequencies. The fact that it is cosine is not important as cosine is just a sine with a phase, that is, a time offset.




In Amplitude Modulation, a signal, such as an audio, is modulated to higher frequencies. It is propagated by an antenna as an electromagnetic signal.




If the two frequencies are almost equal, beats result as one of the output is seen as a low-frequency signal, which is seen as an envelope to a much higher signal, which is about 2 times the frequency of the original signal.




The first csound example will have frequencies of 2000 Hz and 500 Hz. We know the output should be a combination of 1500 Hz and 2500 Hz. We can relate all this to csound opcodes, since multiplying two sines is the same as feeding one oscil into another.




This is the Instrument String. First the linseg opcode is used to create an envelope. The amplitude is 0.7 except at the ends where it goes to 0. Then, two envelopes are created for the cosine waves. Audio for left channel is oscil output of another oscil. The right channel is the sum of two cosines, at 1500 Hz and 2500 Hz.




These are the two tables used here. Table 2 uses GEN 11 which is a cosine signal generator.




This is the output wave file. The two channels are seen to be indeed identical.




The frequency spectrum, shows that there are two frequencies, and they are at 1500 Hz and 2500 Hz.




We can find the period of the wave by selecting the portion of the audio that repeats. It is of width 88 samples. Since there are 44,100 samples in one second this is 44,100 divided by 88 or 500 Hz. This make sence since 1500 Hz is the third harmonic of 500 Hz, and 2500 Hz is the fifth harmonic of 500 Hz.




This is the Instrument String for the case that the two frequencies are nearly identical.


# Main.py

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

# *** Instrument String ***
AmpModEx="""
aenv linseg 0,0.1,0.7,p3-.2,0.7,0.1,0
aenv1 = 0.5*aenv
aenv2 = -.5*aenv
aoscil1 oscil aenv,2000,1
aL oscil aoscil1,1950,1
acos1 oscil aenv1,50,2
acos2 oscil aenv2,3950,2
aR = acos1 + acos2
out aL,aR
"""
instrument('1. AM example',1,AmpModEx)

add(stopIns,startSco)

# *** Table ***
rem('Table 1 uses GEN 10 (sines)')
table('1. Sine wave',1,0,8192,10,1)
rem('Table 2 uses GEN 11 (cosines)')
table('2. Cosine wave',2,0,8192,11,1)

# *** Score ***
rem('Amplitude Modulation')
score(1,0,2)

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



From the output wave file, we can see the low-frequency signal acting as an envelope for the higher modulating signal.


; Tut20
<CsoundSynthesizer>
<CsOptions>
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 10
nchnls = 2
0dbfs = 1
; 1. AM example
instr 1
  aenv linseg 0,0.1,0.7,p3-.2,0.7,0.1,0
  aenv1 = 0.5*aenv
  aenv2 = -.5*aenv
  aoscil1 oscil aenv,2000,1
  aL oscil aoscil1,1950,1
  acos1 oscil aenv1,50,2
  acos2 oscil aenv2,3950,2
  aR = acos1 + acos2
  out aL,aR
endin
</CsInstruments>
<CsScore>
; Table 1 uses GEN 10 (sines)
; 1. Sine wave
f 1 0 8192 10 1
; Table 2 uses GEN 11 (cosines)
; 2. Cosine wave
f 2 0 8192 11 1
; Amplitude Modulation
i 1 0 2
</CsScore>
</CsoundSynthesizer>



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



This is the video of Tutorial 20:



No comments:

Post a 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.