Electronics, Synth, Sound effects, Microcontrollers, , Digital signal processing,AVR, PIC, PIC32,ARM, etc
Thursday, 11 April 2013
Thursday, 4 April 2013
Digital Electronics: Overflow, Overflow Detection and Underflow
Digital Electronics: Overflow, Overflow Detection and Underflow
This video is part of materials on modules taught by Derek Molloy
Wednesday, 3 April 2013
Sinewave Generator with the DAC of the ARM SAM3X -The Code
/* Discrete Computational Methods */ /* Generating/Sample discrete sinusoid */ /* Direct digital synthesis is a common technique for generating waveforms digitally. The principles of the technique are simple and widely applicable. You can build a DDS oscillator in hardware or in software. A DDS oscillator is sometimes also known as a Numerically-Controlled Oscillator (NCO). Usually we use a Circular buffer or FIFO. The NCO function contains a sine look-up tables (LUTs) that perform the following functions: sin(n) = sin(2πn/N) where: n = Address input to the LUT N = Number of samples in the LUT sin(n) = Amplitude of sine wave at (2πn/N) Incrementing n from 0 to N causes the LUT to output one complete cycle of amplitude values for the sine function. The value 2πn/N represents a fractional phase angle between 0 and 2π. The time (t) required to increment n from 0 to N is the period of the sine waveforms produced by the NCO function. The LUT address is incremented once each system clock cycle by an amount equal to the phase input. The phase angle data is accumulated and stored in the phase accumulator register. The output of the phase accumulator register is used to address the LUTs. The frequency (f) of the system clock (fCLK) is fixed. Therefore, the frequency of the sine waves is: f = 1/t = fCLK × phase/2π. */ /* Table Lookup */ /*The table look-up method precomputes the unique samples of every output sinusoid at the start of the simulation, and recalls the samples from memory as needed. Because a table of finite length can only be constructedif all output sequences repeat, the method requires that the period ofevery sinusoid in the output be evenly divisible by the sample period. That is, 1/(fiTs) = ki must be an integer value for every channel i = 1, 2, ..., N. The table that is constructed for each channel contains ki elements. For long output sequences, the table look-up method requires far fewer floating-point operations than any of the other methods, but may demand considerably more memory, especially for high sample rates (long tables). This is the recommended method for models that are intended to emulate or generate code for DSP hardware, and that therefore need to be optimized for execution speed.*/ /* Differential */ /* The differential method uses an incremental (differential) algorithm rather than one based on absolute time. This mode offers reduced computational load, but is subject to drift over time due to cumulative quantization error. Because the method is not contingent on an absolute time value, there is no danger of discontinuity during extended operations (when an absolute time variable might overflow). */ /* Trigonometric Function */ /* If the period of every sinusoid in the output is evenly divisible by the sample period, meaning that 1/(fiTs) = ki is an integer for every output yi, then the sinusoidal output in the ith channel is a repeating sequence with a period of ki samples. At each sample time, the block evaluates the sine function at the appropriate time value within the first cycle of the sinusoid. By constraining trigonometric evaluations to the first cycle of each sinusoid, the block avoids the imprecision of computing the sine of very large numbers, and eliminates the possibility of discontinuity during extended operations (when an absolute time variable might overflow). This method therefore avoids the memory demands of the table look-up method at the expense of many more floating-point operations. */ /* - THE CODE - */ /*Used the proverbial timer interrupt example code, and the old techniques on Direct digital synthesis available at places like http://interface.khm.de/index.php/lab/experiments/arduino-dds-sinewave-generator/ or http://rcarduino.blogspot.co.uk/2012/12/arduino-due-dds-part-1-sinewaves-and.html */ // variables accessed by the interrupt volatile byte adc; volatile byte out_sign; volatile boolean l; float pi = 3.141592; float w ; // ψ float yi ; float phase; int D = 1024; byte sign_samp; byte sin_data[1024]; // sine LUT Array int icounter; int counter2; int testPin = 13; // debugging pin digital pin 13 int testPin2 = 12; // debugging pin digital pin 12 int testPin3 = 11; // debugging pin digital pin 11 int testPin4 = 10; // debugging pin digital pin 11 float a; float b; void setup() { fill_sinewave(); // load memory with sine table pinMode(testPin,OUTPUT); pinMode(testPin2,OUTPUT); pinMode(testPin3,OUTPUT); pinMode(testPin4,OUTPUT); startTimer(TC1, 0, TC3_IRQn, 0x8000); //TC1 channel 0, the IRQ // for that channel and the desired frequency - 32768 -see somewhere // else for the reason why analogWrite(DAC0,0); //Duane B// this is a cheat - enable the DAC } void loop() { counter2++; // if (counter2 >= 0x400) { digitalWrite(13, l = !l); // toggle debugging pin on pin 13 counter2=0; fill_sinewave(); } adc=analogRead (0); // get the adc b=analogRead(1); // get the adc 2 if(b<=511) { b=b*pi/128; } else if (b>=512) { b=b/16; } if (adc<=249) { a=2; } if (adc>=250 && adc<=511) { a=4; } if (adc>=512 && adc<=750) { a=6; } if (adc>=851 && adc<=1024) { a=8; } digitalWrite(12, l = !l); // toggle debugging pin on pin 12 } /* * Here is the table of parameters: * ISR/IRQ TC Channel Due pins TC0 TC0 0 2, 13 TC1 TC0 1 60, 61 TC2 TC0 2 58 TC3 TC1 0 none <- this line in the example above TC4 TC1 1 none TC5 TC1 2 none TC6 TC2 0 4, 5 TC7 TC2 1 3, 10 TC8 TC2 2 11, 12 */ void startTimer(Tc *tc, uint32_t channel, IRQn_Type irq, uint32_t frequency) { pmc_set_writeprotect(false); pmc_enable_periph_clk((uint32_t)irq); TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK1); uint32_t rc = VARIANT_MCK/8/frequency; //8 because we selected TIMER_CLOCK1 above TC_SetRA(tc, channel, rc/2); //50% high, 50% low TC_SetRC(tc, channel, rc); TC_Start(tc, channel); tc->TC_CHANNEL[channel].TC_IER=TC_IER_CPCS; tc->TC_CHANNEL[channel].TC_IDR=~TC_IER_CPCS; NVIC_EnableIRQ(irq); } // TC1 ch 0 void TC3_Handler() { digitalWrite(10, l = !l); // toggle debugging pin on pin 10 TC_GetStatus(TC1, 0); icounter++; // increment index //icounter=icounter + b; // Variable frequency with potentiometer icounter = icounter & 0x3ff; // limit index 0..1023 if( icounter==0x400) { icounter=0; } out_sign=sin_data[icounter]; dacc_write_conversion_data(DACC_INTERFACE, out_sign); } /* Plotting Complex Sinusoids as Circular Motion */ /* */ /* Euler's relation graphically as it applies to sinusoids. A point traveling with uniform velocity around a circle with radius 1 may be represented by */ /* eiφ = cos(φ) + i*sin(φ) */ /* eψt=eψft */ /* in the complex plane, where: t is time and is the number of revolutions per second. e is Euler's number, the base of natural logarithms, i is the imaginary unit, which satisfies i2 = −1, and π is pi, the ratio of the circumference of a circle to its diameter. (1) http://en.wikipedia.org/wiki/Sound (2) http://en.wikipedia.org/wiki/Sound_frequency (3) http://en.wikipedia.org/wiki/Sine_wave (4) https://ccrma.stanford.edu/~jos/Welcome.html (5) http://lionel.cordesses.free.fr/gpages/DDS1.pdf */ void fill_sinewave() { digitalWrite(11, l = !l); // toggle debugging pin on pin 11 w= a*pi; w= w/512; // sine LUT Array D= ox8000(fs)@32Hz. at use 512
//The shape of the stored waveform is 64Hz
// arbitrary, and can be a sinusoid, a square, sawtooth, etc // fill the 1024 byte circular ring buffer array for (D = 0; D <= 0x3ff; D++) { yi= 0x7f*sin(phase); // try yi= 0x7f*sin(phase)-(2*cos(phase)); // increase to 3 ? yi= A*sin(phase)- cos(phase)- cos(phase)- cos(phase); phase=phase+w; // 0 to 2xpi - 1/1024 increments sign_samp=0x7f+yi; // dc offset sign_samp+= b; // Add adc value; Keep it at zero for pure sine sin_data[D]=sign_samp; // write value into array /* */ } digitalWrite(11, l = !l); // toggle debugging pin on pin 11 }http://dubworks.blogspot.co.uk/p/blog-page.html
- Chec my related post with a slightly refined code template
Monday, 1 April 2013
Shive Wave Machine : Similiarities of Wave Behavior
In the past i remember this being the best explanation to some of the basics to understand sound waves better !!
"Shive's role at Bell Labs was more than just a great lecturer: he worked on early transistor technology, inventing the phototransistor in 1950, and the machine he uses in the film is his invention, now called the Shive Wave Machine in college classrooms.
Dr. J.N. Shive of Bell Labs demonstrates and discusses the following aspects of wave behavior:
Reflection of waves from free and clamped ends
Superposition
Standing waves and resonance
Energy loss by impedance mismatching
Reduction of energy loss by quarter-wave and tapered-section transformers
Original audience: college students
Produced at Bell Labs
Footage courtesy of AT&T Archives and History Center, Warren, NJ "
What is sound ;Euler's indentity and Plotting Complex Sinusoids as Circular Motion and Discrete Computational Methods for generating sinusoid signals
* While you are supposed to have knowledge of trigonometry and complex numbers and equations to understand this subject, i’ll do my best to try and make a brief introduction for those who dont.
http://math.tutorvista.com/trigonometry/sine-curve.html
“ What is sound ?! "
Wikipedia (1) says that “Sound is a mechanical wave that is an oscillation of pressure transmitted through a solid, liquid, or gas, composed of frequencies within the range of hearing.”
But we also know that we, humans, can only perceive a certain range of frequencies. To quote : “ The perception of sound in any organism is limited to a certain range of frequencies. For humans, hearing is normally limited to frequencies between about 20 Hz and 20,000 Hz (20 kHz), although these limits are not definite. The upper limit generally decreases with age.”.That gives us a limit to where concentrate our efforts regarding creating sounds, between ( at least) 20 Hz and 20,000 Hz. To be more realistical, not many people can hear above 16 kHz.
* Quote: “ The hertz (symbol Hz) is the SI unit of frequency defined as the number of cycles per second of a periodic phenomenon.”(2)

One of the basic sounds we get is a Sine Wave .
The sine wave is important because is it acoustically unique; it retains its waveshape when added to another sine wave of the same frequency and arbitrary phase and magnitude. It is the only periodic waveform that has this property.
This property leads to its importance in Fourier analysis . (3)
The mathmatical equation to translate a sinewave is
y(t) = A sin (2π f t + φ ) = A sin(ω + φ)
where:
In general, a sine wave is given by the formula
A - .Amplitude is defined as the maximum displacement ( peak deviation) of the function from it's centre position (or zero). It can also be seen as half the distance between the maximum and minimum values of a function. The amplitude of a sine curve is 1.
f - the ordinary frequency, is the number of oscillations (cycles) that occur each second of time.
ω = 2πf - the angular frequency, is the rate of change of the function argument in units of radians per secondHere we will use a floating point
φ - the phase, specifies (in radians) where in its cycle the oscillation is at t = 0.
When φ is non-zero, the entire waveform appears to be shifted in time by the amount φ / ω seconds. A negative value represents a delay, and a positive value represents an advance.
Discrete Time Fourier Transform converts signal from time domain to frequency domain.
Freq =1/Period
Period measured in Seconds (Continuous time)or samples(Discrete time)
Frequency measured in Radians/time or Cycles/time
Plotting Complex Sinusoids as Circular Motion
Euler's relation graphically as it applies to sinusoids. A point travelling with uniform velocity around a circle with radius 1 may be represented by
e is Euler's number, the base of natural logarithms,
i is the imaginary unit, which satisfies i2 = −1, and
π is pi, the ratio of the circumference of a circle to its diameter.
** eiφ = cos(φ) + i*sin(φ) **
** eψt=eψft **
in the complex plane, where:
t is time and is the number of revolutions per second.e is Euler's number, the base of natural logarithms,
i is the imaginary unit, which satisfies i2 = −1, and
π is pi, the ratio of the circumference of a circle to its diameter.
** Discrete Computational Methods **
The three options for generating/Sample the discrete sinusoid signals are :** Table Lookup
The table look-up method pre-computes the unique samples of every outputsinusoid at the start of the simulation, and recalls the samples from
memory as needed. Because a table of finite length can only be constructed
if all output sequences repeat, the method requires that the period of
every sinusoid in the output be evenly divisible by the sample period.
That is, 1/(fiTs) = ki must be an integer value for every channel i = 1, 2, ..., N.
The table that is constructed for each channel contains ki elements.
For long output sequences, the table look-up method requires far fewer floating-point operations than any of the other methods, but may demand considerably more memory, especially for high sample rates (long tables).
This is the recommended method for models that are intended to emulate or generate code for DSP hardware, and that therefore need to be optimized for execution speed.
** Differential
The differential method uses an incremental (differential) algorithm rather than one based on absolute time.This mode offers reduced computational load, but is subject to drift over time due to cumulative quantization error.
Because the method is not contingent on an absolute time value, there is no danger of discontinuity during extended operations (when an absolute time variable might overflow).
** Trigonometric Fcn
If the period of every sinusoid in the output is evenly divisible by the sample period, meaning that1/(fiTs) = ki is an integer for every output yi, then the sinusoidal output in the ith channel is a repeating sequence with a period of ki samples. At each sample time, the block evaluates the sine function at the appropriate time value within the first cycle of the sinusoid. By constraining trigonometric evaluations to the first cycle of each sinusoid, the block avoids the imprecision of computing the sine of very large numbers, and eliminates the possibility of discontinuity during extended operations (when an absolute time variable might overflow). This method therefore avoids the memory demands of the table look-up method at the expense of many more floating-point operations.
(1) http://en.wikipedia.org/wiki/Sound
(2) http://en.wikipedia.org/wiki/Sound_frequency
(3) http://en.wikipedia.org/wiki/Sine_wave
(4) https://ccrma.stanford.edu/~jos/Welcome.html
(5) http://lionel.cordesses.free.fr/gpages/DDS1.pdf
(6) http://www.youtube.com/watch?v=fCAZ7jcO-vc
sinewave generator with the DAC of the ARM SAM3X
On my adventures in Digital signal processing and sound synthesis, i started to do some experiments...
First was to do a sinewave generator with the DAC of the Arduino DUE and the timer interrupt at 4096 samples per second. I know its not much for the ARM core of the SAM3X chip that clocks at 84Mhz, but was a exercise more than anything. This is quite scalable, anyway !
Direct digital synthesis is a common technique for generating waveforms digitally. The principles of the technique are simple and widely applicable. You can build a DDS oscillator in hardware or in software.A DDS oscillator is sometimes also known as a Numerically-Controlled Oscillator (NCO).
Usually we use a Circular buffer or FIFO.
The NCO function contains a sine look-up tables (LUTs) that perform the following functions:
sin(n) = sin(2πn/N)
where:
n = Address input to the LUT
N = Number of samples in the LUT
sin(n) = Amplitude of sine wave at (2πn/N)
Incrementing n from 0 to N causes the LUT to output one complete cycle of amplitude values for the sine function. The value 2πn/N represents a fractional phase angle between 0 and 2π. The time (t) required to increment n from 0 to N is the period of the sine waveforms produced by the NCO function.
The LUT address is incremented once each system clock cycle by an amount equal to the phase input. The phase angle data is accumulated and stored in the phase accumulator register. The output of the phase accumulator register is used to address the LUTs.
The frequency (f) of the system clock (fCLK) is fixed. Therefore, the frequency of the sine waves is:
f = 1/t = fCLK × phase/2π.
As a taster of the code to come (Used the proverbial timer interrupt example code, and the old techniques on Direct digital synthesis available at places like interface.khm.de) .Ill leave you some pictures
Mine is the picture below (1st), below is the output of a grain synth , code available at rcarduino.blogspot.co.uk.
*The sinewave generator code i shall share is not the one at this link above, but an " original " one !.
Also had to mention the wicked BASIC program i been using to help me in my math algorithms called "Decimal BASIC", available at http://hp.vector.co.jp/authors/VA008683/english/.
The sine wave graphic at the top is from it !!
Labels:
32,
Arduino,
Arduino DUE,
ARM,
ARM SAM3X,
C Programming,
DubWorks,
electronics,
SAM3X,
Theory
Subscribe to:
Posts (Atom)




