Chapter 4 Sine Waves

Embed Size (px)

Citation preview

  • 7/15/2019 Chapter 4 Sine Waves

    1/42

    0 - 1

    2010 Texas Instruments Inc

    Practical Audio Experiments using the TMS320C5505 USB Stick

    Sine Waves

    Texas Instruments University Programme

    Teaching Materials

  • 7/15/2019 Chapter 4 Sine Waves

    2/42

    Chapter 4 - Slide 2 2010 Texas Instruments Inc

    Sine Waves

  • 7/15/2019 Chapter 4 Sine Waves

    3/42

    Chapter 4 - Slide 3 2010 Texas Instruments Inc

    Introduction

    DSP can be used to generate sine waves

    Sine waves can be used in audio to:

    Generate musical tones and complex waveforms

    Generate tones for touch phones (DTMF)

    Modulate audio signals (alien voices)

    Control audio effects (chorus/phasing/flanging).

  • 7/15/2019 Chapter 4 Sine Waves

    4/42

    Chapter 4 - Slide 4 2010 Texas Instruments Inc

    Objectives

    To generate sine waves from 10Hz to 16000Hz.

    To introduce the Texas Instruments library of DSPfunctions DSPLIB.

  • 7/15/2019 Chapter 4 Sine Waves

    5/42

    Chapter 4 - Slide 5 2010 Texas Instruments Inc

    Knowledge Required

    Some understanding of fixed-point and floating-pointnumbers is required.

    Details of two useful articles from www.cnx.org are

    given in the References Section.

    http://www.cnx.org/http://www.cnx.org/
  • 7/15/2019 Chapter 4 Sine Waves

    6/42

    Chapter 4 - Slide 6 2010 Texas Instruments Inc

    Sine Wave and FFT

    A sine wave is a pure tone. It only contains onefrequency:

  • 7/15/2019 Chapter 4 Sine Waves

    7/42Chapter 4 - Slide 7 2010 Texas Instruments Inc

    Complex Waveform and FFT

    A complex waveform has several frequencycomponents:

  • 7/15/2019 Chapter 4 Sine Waves

    8/42Chapter 4 - Slide 8 2010 Texas Instruments Inc

    Generating Sine Waves

    There are 3 main ways to generate sine waves:

    Look-up Table

    Recursive Equation

    Taylor Expansion.

  • 7/15/2019 Chapter 4 Sine Waves

    9/42Chapter 4 - Slide 9 2010 Texas Instruments Inc

    Look-up Table

    This is the simplest way to generate a sine wave.

    Put known values into a table:

    Values are read using an offset e.g. sinetable[3];

  • 7/15/2019 Chapter 4 Sine Waves

    10/42Chapter 4 - Slide 10 2010 Texas Instruments Inc

    About Look-up Tables

    Advantages:

    Fast to implement

    Values are always accurate

    Disadvantages:

    Can only be used for exact divisions of sampling

    frequency.

  • 7/15/2019 Chapter 4 Sine Waves

    11/42Chapter 4 - Slide 11 2010 Texas Instruments Inc

    Recursive Equation

    Uses the following mathematical equation:

    The next sine output is derived from the previousvalues

    We shall look at this in more detail in Chapter 7,Infinite Impulse Response (IIR) filters.

    21

    1

    .cos21

    .sin

    )(

    )()(

    zzT

    zT

    nx

    nyzH

  • 7/15/2019 Chapter 4 Sine Waves

    12/42Chapter 4 - Slide 12 2010 Texas Instruments Inc

    Taylor Series

    A sine function can be implemented as a geometricseries:

    where x is the input in radians.

    This method is used by the Texas Instruments DSPLibrary DSPLIB.

    !7!5!3)sin(

    753 xxx

    xx

  • 7/15/2019 Chapter 4 Sine Waves

    13/42Chapter 4 - Slide 13 2010 Texas Instruments Inc

    About Taylor Series

    Advantages:

    Can generate any frequency

    Disadvantages:

    Not as accurate as look-up table because thereare rounding errors

    Care needs to be taken to avoid overflow during

    multiplications.

  • 7/15/2019 Chapter 4 Sine Waves

    14/42Chapter 4 - Slide 14 2010 Texas Instruments Inc

    C Code Implementation

  • 7/15/2019 Chapter 4 Sine Waves

    15/42Chapter 4 - Slide 15 2010 Texas Instruments Inc

    Sine Function in C

    As standard, C comes with the function sin(x) inmath.h.

    This uses floating-point maths.

    It is not efficient for real-time applications.

    A better way is to use DSPLIB.

  • 7/15/2019 Chapter 4 Sine Waves

    16/42Chapter 4 - Slide 16 2010 Texas Instruments Inc

    Introducing DSPLIB

  • 7/15/2019 Chapter 4 Sine Waves

    17/42

    Chapter 4 - Slide 17 2010 Texas Instruments Inc

    About DSPLIB

    Texas Instruments provides a library containing awhole range of useful functions used in DSP:

    Fast Fourier Transform (FFT)

    Sine, Cosine and Tangent

    Exponentials and logs.

    Each function is optimised for the processor, in thiscase the TMS320C55xx.

  • 7/15/2019 Chapter 4 Sine Waves

    18/42

    Chapter 4 - Slide 18 2010 Texas Instruments Inc

    DSP LIB Headers

    When using DSPLIB, you need to add the twofollowing #include statements to your code:

  • 7/15/2019 Chapter 4 Sine Waves

    19/42

    Chapter 4 - Slide 19 2010 Texas Instruments Inc

    DSPLIB Library

    The library file55xdsph.lib

    must be presentin the build.

    DSPLIB forTMS320C5505

    USB Stick.

  • 7/15/2019 Chapter 4 Sine Waves

    20/42

    Chapter 4 - Slide 20 2010 Texas Instruments Inc

    DSPLIB Sine Function

    Is written in TMS320C55xx assembly language.

    The function takes 3 parameters:

    Parameter 1. Address of location containing the frequency

    Parameter 2. Address of location to store calculated sine

    Parameter 3. Always 1.

  • 7/15/2019 Chapter 4 Sine Waves

    21/42

    Chapter 4 - Slide 21 2010 Texas Instruments Inc

    Scaling the sine() function

    Need to convert frequency in Hz to value for sine()function.

    Use a scaling factor of 22368.

  • 7/15/2019 Chapter 4 Sine Waves

    22/42

    Chapter 4 - Slide 22 2010 Texas Instruments Inc

    Magic Numbers

    Where did the magic number 22368 come from?

    The TMS320C5505 is a 16-bit fixed-point processorthat uses:

    32767 to represent 1.000

    32767 to represent 1.000

    Here 22368 represents 0.682 decimal.

    We shall now look at how this magic number wasobtained.

  • 7/15/2019 Chapter 4 Sine Waves

    23/42

    Chapter 4 - Slide 23 2010 Texas Instruments Inc

    DSPLIB sine() function

    The DSPLIB function sine() calculates the sine of anangle.

    The input to the function is a fixed-point number that

    represents an angle: 0 => 0o

    16383 => 90o

    32767 => 180o

    2 * 32767 => 360o

  • 7/15/2019 Chapter 4 Sine Waves

    24/42

    Chapter 4 - Slide 24 2010 Texas Instruments Inc

    Sine 90o

    To generate a waveform using 4 values we use: sin 0

    o

    sin 90o

    sin 180o

    sin 270o.

    If Fs = 48000 Hz, the frequency generated will be:

    48000/4 = 12000 Hz.

  • 7/15/2019 Chapter 4 Sine Waves

    25/42

    Chapter 4 - Slide 25 2010 Texas Instruments Inc

    Sine 45o

    To generate a waveform using 8 value use: sin 0

    o

    sin 45o

    sin 90o

    sin 135o

    etc.

    If Fs = 48000 Hz, the frequency generated will be:

    48000/8 = 6000 Hz.

  • 7/15/2019 Chapter 4 Sine Waves

    26/42

    Chapter 4 - Slide 26 2010 Texas Instruments Inc

    Generate 1 Hz Sine Wave

    To generate a 1 Hz sine wave we work backwards: 48000/value = 1 Hz

    value = 1/48000

    There corresponding angle will be:

    360o/48000 = 0.0075

    o

    To implement a 1 Hz sine wave we use:

    0o, 0.0075

    o, 0.015

    o, 0.0225

    o, 0.030

    oetc.

  • 7/15/2019 Chapter 4 Sine Waves

    27/42

    Chapter 4 - Slide 27 2010 Texas Instruments Inc

    Fixed-Point Implementation

    For 1 Hz we require each angle to be multiples of: 360

    o/48000 = 0.0075

    o

    For 1 Hz using fixed-point using DSPLIB we require:

    2 * 32767 / 48000

  • 7/15/2019 Chapter 4 Sine Waves

    28/42

    Chapter 4 - Slide 28 2010 Texas Instruments Inc

    Scaling Factor

    We can use the value for 1 Hz as a scaling factortocalculate other frequencies:

    SCALING FACTOR = 360o/48000 = 0.0075

    o

    For 2 Hz:

    2 * SCALING FACTOR = 2 * 360o/48000 = 0.015

    o

    For 10 Hz:

    10 * SCALING FACTOR = 10 * 360o/48000 = 0.075

    o

  • 7/15/2019 Chapter 4 Sine Waves

    29/42

    Chapter 4 - Slide 29 2010 Texas Instruments Inc

    Scaling Factor Calculation

    The fixed-point scaling factor is:

    In fixed-point maths, to divide by 48000 is awkward

    However, to divide by 32768 is easy because 32768= 215

    Example: To divide 3FFFFFFFh by 32768d shift right15 places. Result = 7FFFh

    In C code, divide by 32768 is implemented as >> 15.

    48000

    32767*2

  • 7/15/2019 Chapter 4 Sine Waves

    30/42

    Chapter 4 - Slide 30 2010 Texas Instruments Inc

    Scaling Factor Calculation

    The fixed-point scaling factor is derived as follows:

    The divide by 32768 is implemented as >>15

    Here 2/32768 is implemented as >>14.

    The scaling factor used is therefore 22368.

    32768

    22368*2

    48000

    32767*2

  • 7/15/2019 Chapter 4 Sine Waves

    31/42

    Chapter 4 - Slide 31 2010 Texas Instruments Inc

    Introduction to Laboratory

  • 7/15/2019 Chapter 4 Sine Waves

    32/42

    Chapter 4 - Slide 32 2010 Texas Instruments Inc

    USB Stick Setup TMS320C5505

    USB to PC

    Headphones

    USB Stick

  • 7/15/2019 Chapter 4 Sine Waves

    33/42

    Chapter 4 - Slide 33 2010 Texas Instruments Inc

    Installing the Application

    Use the code given in Application 4, Sine Waves

    Follow the steps previously given in Chapter 1 toset up the new project.

  • 7/15/2019 Chapter 4 Sine Waves

    34/42

    Chapter 4 - Slide 34 2010 Texas Instruments Inc

    Create New Project

  • 7/15/2019 Chapter 4 Sine Waves

    35/42

    Chapter 4 - Slide 35 2010 Texas Instruments Inc

    Files Used in Project

  • 7/15/2019 Chapter 4 Sine Waves

    36/42

    Chapter 4 - Slide 36 2010 Texas Instruments Inc

    Console

    Sampling frequency and Gain are shown in theConsole window.

  • 7/15/2019 Chapter 4 Sine Waves

    37/42

    Chapter 4 - Slide 37 2010 Texas Instruments Inc

    Experiments

  • 7/15/2019 Chapter 4 Sine Waves

    38/42

    Chapter 4 - Slide 38 2010 Texas Instruments Inc

    Change the Headphone Volume

    Reduce gain from 10000 to 5000.

  • 7/15/2019 Chapter 4 Sine Waves

    39/42

    Chapter 4 - Slide 39 2010 Texas Instruments Inc

    Change the Frequencies

    Rather than 200 Hz and 500 Hz, use two musicalnotes:

    A = 440 Hz C = 523 Hz

  • 7/15/2019 Chapter 4 Sine Waves

    40/42

    Chapter 4 - Slide 40 2010 Texas Instruments Inc

    Change the Sampling Frequency

    Change the sampling frequency to 24000 Hz.

    The output frequencies will have changed.

    You will need to alter the scaling factor insinewaves.c

  • 7/15/2019 Chapter 4 Sine Waves

    41/42

    Chapter 4 - Slide 41 2010 Texas Instruments Inc

    Questions

    What are 3 ways to generate sine waves?

    Which method is best suited to the TMS320C5505USB Stick?

    What are 3 applications of sine waves?

  • 7/15/2019 Chapter 4 Sine Waves

    42/42

    References

    TMS320C55xx DSP Library ProgrammersReference. SPRU 422.

    Digital Signal Processing with C and theTMS320C30 by Rulph Chassaing.ISBN 0-471-55780-3.

    www.cnx.org Fixed Point Arithmetic and Format(m10919) by Hyeokho Choi.

    www.cnx.org Fixed Point Arithmetic (m11054) byHyeokho Choi.

    http://www.cnx.org/http://www.cnx.org/http://www.cnx.org/http://www.cnx.org/