1.48k likes | 1.49k Views
Learn about automatic gain/volume control, estimation of signal energy, moving average, and amplitude panning in audio processing.
E N D
Microcomputer Systems 1 Audio Processing Problems and Solutions
Automatic Gain/Volume Control • One of the simplest operations that can be performed in a DSP on an audio signal is volume gain and attenuation control (AGC). • For fixedpoint math, this operation can be performed by • multiplying each incoming sample by a fractional 16 bit value number between 0x0000…. and 0x7FFF…. or • using a shifter to multiply or divide the sample by a power of 2. • When increasing/degreasing the gain of a signal, the programmer must be aware of overflow, underflow, saturation, and quantization noise effects. Veton Këpuska
Estimation of the Energy of the Signal • Algorithm: • Keep track of the maximum energy of the input signal. if (abs(in_sample) > myMax) { myMax = abs(in_sample); } • Ajust the Gain to cover 80% of the overall dynamic range of the output. new_target_gain = 0.8*MAX_RANGE/myMax; • Compute actual gain factor based on some empirically defined function that performs necessary smoothing based on desired responsiveness and smoothness of the gain. gain = compute_gain(gain, new_target_gain); • Apply Gain: out_sample = gain*in_sample; • Reset the gain based on some criteria (e.g., myMaxGain factor) Veton Këpuska
Gain Update Function float compute_gain(gain, new_gain) { // Linear interpolation float g, alpha = 0.2; // computed gain will adjust gain // each time is called by 20% toward // target gain g = (1-alpha)*gain + alpha*new_gain; return (g); } Veton Këpuska
AGC • AGC is never used on the sample based measurement. • AGC is frame-based • How to compute efficiently energy of a frame? Veton Këpuska
Moving Average • For each sample i must compute moving average of size N: sum = 0; for (j=0; j<N; j++) sum=in(j); out = sum/N; • Must keep track of the buffering of size N: memcpy( &in[0], &in[1], N-1); // shifting N-1 samples in[N-1] = new_sample; // New sample Veton Këpuska
Efficient Moving Average Veton Këpuska
Moving Average • Note that the algorithm needs to use slightly modified implementation of initialization routine for samples less than the number of averaging samples N (i.e., 64). Veton Këpuska
Amplitude Panning of Signals to Left or Right Stereo Field Reference: Using The Low Cost, High Performance ADSP-21065L Digital Signal Processor For Digital Audio Applications Dan Ledger and John Tomarakos DSP Applications Group, Analog Devices, Norwood, MA 02062, USA
Amplitude Panning of Signals to Left or Right Stereo Field • In many applications, the DSP may need to process two (or more) channels of incoming data, typically from a stereo A/D converter. • Two-channel recording and playback is still the dominant method in consumer and professional audio and can be found in mixers and home audio equipment. V. Pulkki [22] demonstrated placement of a signal in a stereo field (see Figure 4 below) using Vector Base Amplitude Panning. • The formulas presented in Pulkki’s paper for a two-dimensional trigonometric and vector panning will be shown for reference. Veton Këpuska
Amplitude Panning of Signals to a Left or Right Stereo Field • Normally, the stereo signal will contain an exact duplicate of the sampled input signal, although it can be split up to represent two different mono sources. • Also, the DSP can also take a mono source and create signals to be sent out to a stereo D/A converter. • Typical audio mixing consoles and multichannel recorders will mix down multiple signal channels down to a stereo output field to match the standardized configuration found in many home stereo systems. Figure 25 is a representation of what a typical panning control ‘pod’ looks like on a mixing console or 8-track home recording device, along with some typical pan settings: Veton Këpuska
Pan Control • Three Typical Pan Control Settings of a Mono Source To A Stereo Output Field L L L R R R Full Right Pane Full Left Pane Center Mix Veton Këpuska
“Source of the Sound” • To give the listener a sense of location within the output stereo filed, the DSP can simply perform a multiplication of the algorithmic result on both the left and right channel so that it is perceived from coming from a phantom source. Virtualsource x 0 -0 y Panning of Two-Channel StereophonicAudio; Derived by Blumlein, Bauer and Bernfeld [26] Veton Këpuska
“Source of the Sound” • Pulkki’s Method [26] For Vector Panning of Two-Channel Audio Virtualsource x -0 0 p IR IL gRIR gLIL y Veton Këpuska
Source of the Sound • To create a panning effect of an audio channel to a particular position in the stereo output field, the programmer can use the Stereophonic Law of Sines, or the Tangent Law equation (Pulkki, Blumlein and Bauer[22], see Figure) where gL and gR are the respective gains of the left and right channels. • This is valid if the listener’s head is pointing straight ahead. If the listener turns the head to follow the virtual source, the Tangent Law equation as described by Pulkki [derived by Bernfeld, 26] is modified as: Veton Këpuska
Source of the Sound • Assuming fixed point signed fractional arithmetic where signals are represented between 0 (0x0000…) and 0.99999 (0x7FFF…), the DSP programmer needs simply to multiply each signal by the calculated gain. • Using Pulkki's Vector Base Amplitude Panning method as shown in the slide 11, the position p of the phantom sound source is calculated from the linear combination of both speaker vectors: Veton Këpuska
Source of the Sound • The output difference I/O equations for each channel are simply: Veton Këpuska
Left Pan: If the virtual source is panned completely to the left channel, the signal only comes out of the left channel and the right channel is zero. When the gain is 1, then the signal is simply passed through to the output channel. GL= 1 GR= 0 Right Pan: If the virtual source is panned completely to the right channel, the signal only comes out of the right channel and the left channel is zero. When the gain is 1, then the signal is simply passed through to the output channel. GL= 0 GR= 1 Center Pan: If the phantom source is panned to the center, the gain in both speakers are equal. GL= GR Arbitrary Virtual Positioning: If the phantom source is between both speakers, the tangent law applies. The resulting stereo mix that is perceived by the listener would be off-scale left/right from the center of both speakers. Some useful design equations [26] are shown below: Vector Based Amplitude Panning Summary Veton Këpuska
Table Lookup • If DSP processor/IDE does not support trigonometric functions then table lookups can be stored with pre- computed panning values for number of angles. • Table below shows left and right channel gains required for the desired panning angle: Veton Këpuska
Professional and Consumer use equalizers to adjust the amplitude of a signal within selected frequency ranges. In a Graphic Equalizer, the frequency spectrum is broken up into several bands using band-pass filters. Setting the different gain sliders to a desired setting gives a ‘visual graph’ (Figure in this slide) of the overall frequency response of the equalizer unit. The more bands in the implementation yields a more accurate desired response. Graphic Equalizers Veton Këpuska
Graphic Equalizer • Analog equalizers typically uses passive and active components. Increasing the number of bands results in a large board design. • When implementing the same system in a DSP, however, the number of bands is only limited by the speed of the DSP (MIPs) while board space remains the same. Resisters and capacitors are replaced by discrete-time filter coefficients, which are stored in a memory and can be easily modified. • Figure 33 in the next slide shows and example DSP structure for implementing a 6 band graphic equalizer using second order IIR filters. • The feedforward path is a fixed gain of 0.25, while each filter band can be multiplied by a variable gain for gain/attenuation. • There are many methods of implementation for the second order filter, such as using ladder structures or biquad filters. Filter coefficients can be generated by a commercially available filter design packages (e.g., MATLAB), where filter coefficients (e.g., A and B ) can be generated in for the following 2nd order transfer function and equivalent I/O difference equations: Veton Këpuska
Graphic EqualizerSecond Order IIR Filter • Direct form II implementation equations are given below (Ch4-Discrete-Time Signal Processing Framework.pptx) • and ARMA implementation equation is given below Veton Këpuska
Block Diagram of Graphic Equalizer • DSP Implementation of a Digital Graphic Equalizer g1 2nd Order IIR Band 1 Filter x[n] g2 gMaster 2nd Order IIR Band 2 Filter gi y[n] 2nd Order IIR Band i Filter gN 2nd Order IIR Band N Filter Veton Këpuska
Time-Delay Digital Audio Effects • Background theory and basic implementation of variety of time-based digital audio effects will be examined. • The figure below shows some algorithms that can be found in digital audio effects processor. • Multiple reflection delay effects using delay lines will be discussed first then more intricate effects such as: • Chorusing (animation of the basic sound by mixing it with two slightly detuned copies of itself) • Flanging (is an audio process that combines two copies of the same signal, with the second delayed slightly (less than 20 msec), to produce a swirling effect • Pitch shifting • Reverberation (Reverberation is the persistence of sound in a particular space after the original sound is removed. When sound is produced in a space, a large number of echos build up and then slowly decay as the sound is adsorbed by the walls and air, creating reverberation, or reverb.) Veton Këpuska
Typical Signal Chain for Audio Multi-Effects Processors By-pass Control Digital Delay/Reverb Distortion/Overdrive Chorus/Flanger Comp-ressor InputSignal Equalizer OutputSignal Veton Këpuska
Digital Delay • The Digital Delay is the simplest of all time delay audio effects. The delay effect is often the basis to produce more intricate effects such as flanging and chorusing, which vary the delay time on-the-fly. It is also used in reverberation algorithms to produce early reflections and recursive delays. • To create reflection digitally, DSP delay effects units encode the input signal and store it digitally in a delay-line buffer until it is required at the later time where it is decoded back to analog form [17]. • The DSP can produce delays in a variety of ways. Delay units can produce stereo results and multiple-tapped delayed results [7]. Many effects processors implement a delay and use it as a basis for producing multi-tap and reverb effects. Multi-tapped signals can be panned to the right, left or mixed together to give the listener the impression of the stereo echo bouncing from one side to the other. Veton Këpuska
Digital Delay Line • Delay Line with buffer size D Z-D x[n] y[n] = x[n-D] Veton Këpuska
Signal Generation • Methods of signal generation for wavetable synthesis, delay-line modulation and tremolo effects can be produced by: • Using a periodic lookup of a signal stored in the DSP’s data memory. • Generating signal in real-time • Wavetable Generators can be used to implement many timedelay modulation effects an amplitude effects such as the • chorus, • flanger, • vibrato (Vibrato is a musical effect where the pitch or frequency of a note or sound is quickly and repeatedly raised and lowered over a small distance for the duration of that note or sound. Vibrato is naturally present in the human voice, and is used to add expression and vocal-like qualities to instrumental notes.), and • tremolo (Tremolo is the rapid repetition of one note in music or a rapid alternation between two or more notes). • The figure in the next slide shows some of the more common signals that can be easily stored in memory for use in audio applications. Veton Këpuska
Example of Signals Veton Këpuska
How to Generate Pure Tone Signals • Digital Sinusoidal Signal: • A – amplitude • f0 – frequency of the sinusoid in Hz • F0 – normalized frequency in cycles per sample defined as: Veton Këpuska
Generation of Basic Signals • The range of normalized frequency is - 1/2≤F0≤1/2 since 2|f0|≤fs - according to sampling theorem. • Digital frequency ω0, in radians per sample is defined as: Veton Këpuska
VisualDSP++ Library Functions Veton Këpuska
Notes/Pitch Frequency A brief overview of Notes and their frequencies
Notes/Pitch • Note frequency (hertz) • Technically, music can be composed of notes at any arbitrary frequency. Since the physical causes of music are vibrations of mechanical systems, they are often measured in hertz (Hz), with 1 Hz = 1 complete vibration cycle per second. • For historical and other reasons especially in Western music, only twelve notes of fixed frequencies are used. These fixed frequencies are mathematically related to each other, and are defined around the central note, A4. The current "standard pitch" or "concert pitch" for this note is 440 Hz. • A4 is the 440 Hz tone that serves as the standard for musical pitch. A440 is the musical note A above middle C (A4). Veton Këpuska
Notes/Pitch Frequency • The note naming convention specifies a letter, any sharp/flat, and an octave number. • Any note is exactly an integer number of half-steps away from central A (A4). Let this distance be denoted n. Then, C, D, E, F, G, A, B, C, B, A, G, F, E, D, C Veton Këpuska
Notes/Pitch Frequency • For example, let's find the frequency of the C above Middle A (C5). There are +3 half-steps between A4 and C5 • A — (1) → A♯— (2) → B — (3) → C • A♯ - is called A - Sharp • It is important to keep the sign of n in mind. For example, the F below Middle A is F4. There are -4 half-steps: • A — (1) → Ab — (2) → G — (3) → Gb — (4) → F ... each of these is descending the scale. Thus: • Ab – is called A - Flat Veton Këpuska
Octaves and Notes • Finally, it can be seen from this formula that octaves (n=12) automatically yield factors of two times the original frequency (in fact this is the means to derive the formula, combined with the notion of equally-spaced intervals). • For use with the MIDI (Musical Instrument Digital Interface) standard, a frequency mapping is defined by: • For notes in an A440 equal temperament, this formula delivers the standard MIDI note number. Any other frequencies fill the space between the whole numbers evenly. This allows MIDI instruments to be tuned very accurately in any micro-tuning scale, including non-western traditional tunings. Veton Këpuska
Implementation Approaches • Most high level languages such a C/C++ have build in support to generate trigonometric functions. • Real-time Embedded System Software Engineers who program DSP algorithms mostly in assembly do not have the flexibility of a high level language when generating signals. Various methods proposed by Crenshaw [8], Orfanidis [2] and Chrysafis [39] can be used for generating sinusoidal/random signals in a DSP. Signal generation can be achieved by: • Making a subroutine/function call to a • Taylor Series function approximation for trigonometric signals, • Uniform/Gaussian random number generator routine for random white noise generation. • Using a table lookup • Using hold/linear interpolation operations between consecutive locations in the wavetable to increase the resolution of the stored signal. Veton Këpuska
Implementation Approaches • The advantage of using a wavetable to generate a signal: • It is trivial to generate signal simply by performing a memory read from the buffer, therefore saving DSP cycle overhead. • The wavetable can be implemented as a circular buffer so that the signal stored is regenerated over and over. • The larger the buffer, the purer the signal that can be generated. With larger internal memory sizes integrated on many DSPs or the use of low cost commodity SDRAM, the option of using a look-up table is more easily achievable than in the past. Veton Këpuska
Implementation Approaches • To save memory storage, the size of the table can be reduced by a factor of 2, and as suggested above, the DSP can interpolate between 2 consecutive values. For example, a wavetable buffer can contain 4000 locations to represent 1 period of a sine wave, and the DSP can interpolate in between every value to produce 8000 elements to construct the signal. This is not a bad approximation for generating a decent sounding tone • What is the best way to progress through the table? • The general recommendation for accessing data from the table would be to declare the wavetable in the DSP program as a circular buffer instead of as a linear buffer (see some examples in Figure in the next slide). This will allow the signal to be replayed over and over without the program having to check to see if the pointer needs to be reset. Veton Këpuska
Visualization of Circular Buffer Pointer Position Circular Buffer Pointer Position … 2 1 0 D/2 D Veton Këpuska
Implementation Approaches • Two methods can be used to progress through the lookup table: • Sample-Rate Dependent Update: One method for updating a wavetable pointer is sample-rate dependent update, where a new lookup value is generated every time the sample processing algorithm is entered (typically via an interrupt service routine). This synchronization with the sample rate will not introduce possible aliasing artifacts in implementing delay line modulation. • DSP Timer Expire Update: Another method, would be to update the value in the table using the DSP’s on chip programmable timer. Every time the timer expires and resets itself, the timer ISR can update the pointer to the wavetable buffer. This method allow movement through a table that is not relative to the converter's sampling rate, allowing for more flexible and precise timing of signal generation or delay-line modulation. Veton Këpuska
Implementation Approaches • For certain digital audio effects such as flanging/chorusing/pitch shifting, lookup table updates can be easily achieved using the programmable timer as well as via the audio processing ISR. • Delay-line modulation value can be easily updated by using: • The programmable timer or • An interrupt counter,to process the parameter used to determine how far back in the delay-line buffer the DSP's data addressing unit needs to fetch a previously stored sample. • A sine wavetable can be used to implement many time delay modulation effects and amplitude effects such as the • chorus, • flanger, • vibrato, and • tremolo. • Random Low frequency oscillator (LFO) Tables can be used to implement realistic chorus effects [2]. Using a sawtooth wavetable will be useful for shifting the pitch of a signal [16]. We will look at these examples in more detail in subsequent sections. Veton Këpuska
Digital Delay:Single Reflection Delay • Implementation of a Digital Delay with Single Tap • To implement a single reflection of an input signal, the following difference equation can be used: h[n] 1 *x[n-D] Z-D x[n] y[n] 0 D Veton Këpuska