Sunday, May 25, 2014

25. Filtered Noise


A noise source is used in all the examples. A Butterworth is a particular class of filters; they provide good audio quality. The particular filters, we shall use, are 2nd order. The higher the order, the more accurate but with more computations. With other csound opcodes, you can build any kind of filter.




There are four kinds of filter you can have. The first is the low-pass, which passes frequencies below some frequency, and rejects, that is, attenuates, higher frequencies.




The high-pass filter works in the opposite manner, to the low-pass, and lets the higher frequencies pass.




The third kind of filter is the band-pass. This let's frequencies, in a certain range pass, while, rejecting those outside a range. Here, we have to give the frequency corresponding to the middle of band, as well as the bandwidth.




The band-reject filter is opposite to the band-pass. This passes frequencies outside the range of the band.




Here, we have instrument strings for Noise, to serve as reference, and a low-pass filtered noise.




These are strings showing the other 3 band filters.




Finally, the reference, and the four filtered noise signals are added to different instruments.




This creates a score, calling each instrument for a 2 second duration.


# Filters.py

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

# *** Instrument Strings ***
WhiteNoiseString="""
asig rand 0.6
out asig, asig
"""

LowPassString="""
asig rand 0.6
alp  butterlp asig, 1000 ;passing frequencies below 1 KHz
out alp, alp
"""

HighPassString="""
asig rand 0.6
ahp  butterhp asig, 500 ;passing frequencies above 500 Hz
out ahp, ahp
"""

BandPassString="""
asig rand 0.6
abp  butterbp asig, 1000, 100 ;passing band from 950 to 1050 Hz
out abp, abp
"""

BandRejectString="""
asig rand 0.6
abr  butterbr asig, 10000, 10000 ;rejecting band from 5 kHz to 15 kHz
out abr, abr
"""

instrument('White Noise',1,WhiteNoiseString)
instrument('Low Pass',2,LowPassString)
instrument('High Pass',3,HighPassString)
instrument('Band Pass',4,BandPassString)
instrument('Band Reject',5,BandRejectString)

addTag(stopIns,startSco)

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

# *** Score ***
rem('Noise Filtering Score')
for i in range(5): score(i+1,2*i,2)

addTag(stopSco,stopSyn)
writeRun('Tut25')




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



This is the video of Tutorial 25:


Wednesday, May 21, 2014

24. rand, buzz and gbuzz


rand, buzz and gbuzz are three opcodes producing frequency rich signals. They usually have to be filtered to remove frequencies, a topic covered later.




The rand opcode generates a random sequence. All the random values are between minus and positive of the first parameter, and the only required one.




This String uses a rand opcode, to generate a sequence between negative .5 and positive .5.




A noise signal will have a nearly uniform frequency spectrum.




The buzz opcode uses a sine input frequency, to create a signal with many harmonics. In xcps we give the fundamental frequency, and then we give the number of harmonics we need.




In this String using the buzz opcode, we request 3 harmonics of 440 Hz. The parameter value 1 indicates the table where the sine function is located, for our example.




In the frequency spectrum, we can see that there are three frequencies, 440 Hz, 2 times 440 Hz, and 3 times 440 Hz. All three have the same magnitude.




The gbuzz opcode creates a set of harmonics based on a cosine wave. We have to give it the fundamental frequency, number of harmonics in the output and the lowest harmonic that will be in the output. In addition, there is a parameter, kmul, which is a multiplying factor, controlling how harmonics increase or decrease.




This Instrument String, uses gbuzz to create 3 harmonics of 440 Hz, but we start with the 2nd harmonic (2 times 440 Hz) and the higher frequencies damp off with a multiplying factor of 0.7. We use the cosine table, which is numbered 2, in our program.


# Main.py

from moduleCsound import *
addTag(startSyn,startOpt,stopOpt,startIns)
header()

# *** Instrument Strings ***

randStr="""
asig rand 0.5
out asig
"""

buzzStr="""
asig buzz 1, 440, 3, 1
out asig 
"""

gbuzzStr="""
asig gbuzz 1, 440, 3, 2, 0.7, 2
out asig
"""


instrument('rand',1,randStr)
instrument('buzz',2,buzzStr)
instrument('gbuzz',3,gbuzzStr)

addTag(stopIns,startSco)

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

# *** Score ***
score(1,0,3)
score(2,3,3)
score(3,6,3)

addTag(stopSco,stopSyn)
writeRun('Tut24')



This shows the three frequencies starting at 880 Hz (2 times 440 Hz) which are reducing at the rate of 0.7.


; Tut24.csd
<CsoundSynthesizer>
<CsOptions>
</CsOptions>
<CsInstruments>
sr = 44100
ksmps = 10
nchnls = 1
0dbfs = 1
; rand
instr 1
  asig rand 0.5
  out asig
endin
; buzz
instr 2
  asig buzz 1, 440, 3, 1
  out asig 
endin
; gbuzz
instr 3
  asig gbuzz 1, 440, 3, 2, 0.7, 2
  out asig
endin
</CsInstruments>
<CsScore>
; 1. Sine wave
f 1 0 8192 10 1
; 2. Cosine wave
f 2 0 8192 11 1 1
i 1 0 3
i 2 3 3
i 3 6 3
</CsScore>
</CsoundSynthesizer>



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



This is the video of Tutorial 24:


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.