28
MASTER’S THESIS 2002:215 CIV MASTER OF SCIENCE PROGRAMME Department of Computer Science and Electrical Engineering Division of Computer Engineering 2002:215 CIV • ISSN: 1402 - 1617 • ISRN: LTU - EX - - 02/215 - - SE Design and Implementation of a Guitar Tuner for a Mobile Phone HENRIK AHLM MATHIAS JEPPSSON

Design and implementation of a guitar tuner for a mobile phoneltu.diva-portal.org/smash/get/diva2:1017313/FULLTEXT01.pdfDesign and implemen tation of a guitar tuner for a mobile phone

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

  • MASTER’S THESIS

    2002:215 CIV

    MASTER OF SCIENCE PROGRAMME

    Department of Computer Science and Electrical EngineeringDivision of Computer Engineering

    2002:215 CIV • ISSN: 1402 - 1617 • ISRN: LTU - EX - - 02/215 - - SE

    Design and Implementationof a Guitar Tuner for

    a Mobile Phone

    HENRIK AHLMMATHIAS JEPPSSON

  • Design and implementation of a guitar tuner for

    a mobile phone.

    Henrik Ahlm

    [email protected]

    Mathias Jeppsson

    [email protected]

    June 5, 2002

  • 2(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    Abstract

    The intention of this thesis is to investigate the possibility to implement a

    guitar tuner, using only an ordinary mobile phone as a platform. Samples are

    read from the microphone already located in the mobile, then processed using

    available resources and �nally the result is presented on the display.

    Although the outcome of the thesis can be seen as a success, there is room

    for improvements and alternative solutions.

  • 3(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    Preface

    This thesis is part of the Master of science education we began in the spring of

    1997, at Luleå University of Technology (LTU). The thesis has been supervised

    by Per Lindgren, at the computer science and electrical engineering department

    (EISLAB), and Andreas Göransson, at Sony Ericsson.

    Little did we know that we would �nish our education in the far south

    of Sweden, more than 1500 km away from our university, but it has been an

    interesting journey from the beginning to the end.

    Even though there has been a great many people involved, giving us a helping

    hand or an encouraging word when needed, there are those that has shouldered

    more responsibility than they had to. We especially want to show our gratitude

    towards Andreas Göransson, our tutor at Sony Ericsson, and Daniel Linåker,

    without whom we would not have enjoyed our time at Sony Ericsson as much

    as we did.

    Henrik Ahlm & Mathias Jeppsson

    Lund, Sweden

    February 2002

  • 4(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    Contents

    1 Introduction 5

    1.1 Problem description . . . . . . . . . . . . . . . . . . . . . . . . . 5

    1.2 Problem analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . 5

    1.3 System description . . . . . . . . . . . . . . . . . . . . . . . . . . 5

    2 Background 7

    2.1 Electronic tuners . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

    2.2 Musical notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

    2.3 Tones and overtones . . . . . . . . . . . . . . . . . . . . . . . . . 8

    2.4 Pulse Code Modulation . . . . . . . . . . . . . . . . . . . . . . . 9

    2.5 Floating- vs. �xed-point numerics . . . . . . . . . . . . . . . . . . 9

    3 Method 11

    3.1 Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11

    3.2 Frequency-tracking loops . . . . . . . . . . . . . . . . . . . . . . . 11

    3.2.1 Quadricorrelator . . . . . . . . . . . . . . . . . . . . . . . 12

    3.2.2 Balanced Quadricorrelator . . . . . . . . . . . . . . . . . . 13

    3.3 Sine wave generation . . . . . . . . . . . . . . . . . . . . . . . . . 14

    3.4 Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

    3.4.1 Floating-point precision . . . . . . . . . . . . . . . . . . . 16

    3.4.2 Fixed-point precision . . . . . . . . . . . . . . . . . . . . . 18

    3.5 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

    4 Result and discussion 23

    5 Conclusions 24

    6 Future work 25

    A Glossary 26

  • 5(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    1 Introduction

    To make an instrument sing in a fashion that brings pleasure to the ear, not only

    demand a person skilled in playing it, but also someone that knows how to tune it

    to perfection. Thus, an artist playing an instrument that is not tuned according

    to the audience, will never be able to really catch the attention of people that

    are listening, and the guitar is no exception from this rule. However, there is

    more than one method available when a guitar needs to be tuned.

    As mentioned, there are many ways to tune a guitar, by ear alone or by using

    some kind of instrument that will help the musician on the way. The instrument

    may be something as simple as a tuning-fork, that provide a reference frequency,

    or as complicated, from the constructions point of view, as an electronic device.

    Even though the �rst method may be preferred by some musicians, the second

    one has become increasingly popular with the introduction of low cost guitar

    tuners. Besides, trying to tune a guitar with only experience as a helping hand,

    is not even an option for everyone, in particular those that recently picked up

    an interest in playing the guitar.

    1.1 Problem description

    The goal of this thesis is to utilise the resources of an ordinary mobile phone to

    construct a guitar tuner. The tuner is to keep the user continuously updated

    about changes in the tone he or she is playing (i.e. continuously updating the

    display). Once the tuner indicate that the plucked string has been tuned, the

    tone played has to correspond to the correct tone, plus a reasonable error limit.

    The error tolerance is to be decided by the limitations of the human ear. In

    other words, as long as the deviation is within the error limits, a person tuning

    his guitar should agree that his instrument is tuned.

    1.2 Problem analysis

    The �rst step is to �nd a suitable algorithm and run simulations to verify as-

    sumptions made while selecting this particular algorithm. After a thorough

    veri�cation, an implementation in a real mobile phone will follow. The �rst

    problem presenting itself, when it comes to implementation, is sampling and

    temporary storage of the signal (the sound of the guitar string). Finally, the

    algorithm will be implemented to verify whether the mobile phone can cope

    with all the calculations necessary to keep the display continuosly updated, or

    at least the feeling that it is, with relevant information for the user.

    1.3 System description

    In general, all electronic tuning devices share the same basics, namely a micro-

    phone to pick up the sound of the instrument, a core that can map the sound

    to useful information, and some mean of presenting the result to the user.

    In a mobile phone you have everything needed to make a guitar tuner. The

    microphone to use is the same one that the user usually speaks into, the core

    consist of the chipset and software in the mobile, and the display can be used

    to present the result to the user.

  • 6(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    Microphone CORE

    DISPLAY

    - +

    Figure 1: A simpli�ed system overview.

    Even though the whole system is fairly complicated, it can be simpli�ed into

    three basic elements, as can be seen in �gure 1. While the microphone and the

    display is hardware only, the core consists of both hardware and software. This

    thesis is concentrated around the software in the core, and more speci�cly the

    software needed to solve the problems described in section 1.1.

  • 7(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    2 Background

    This section contains information that will aid in understanding the thesis as a

    whole. A short introduction to electronic tuners is �rst presented, followed by

    some general theory about music and sound. A few notes on how to make the

    implementation more e�cient is also included.

    2.1 Electronic tuners

    Electronic tuners provide an easy way of tuning an instrument to a desired pitch.

    They commonly come in two di�erent shapes, called chromatic and guitar. The

    chromatic tuner is a general purpose tuner, able to tune any tone, while the

    guitar tuner is only able to tune the basic tones of a guitar or bass.

    This thesis is aimed at the latter, even though the methods should be suitable

    for both kinds of tuners.

    2.2 Musical notes

    The modern western musical scale consists of 12 tones, denoted A, A#, B, C,

    C#, D, D#, E, F , F#, G and G#. These 12 tones form an octave. The

    tones in the succeeding octave have double frequency relative to the tones in

    the preceding octave. That is, if A has a frequency of 110 Hz, A in succeeding

    octave has a frequency of 220 Hz. The tones are related by,

    f = fbase � 2�tones=12 (2.1)

    where f is the frequency of interest, fbase is the base frequency, generally 440Hz

    and �tones the number of tones relative to base. Hence, if the base is anA, with frequency 440Hz, D in the same octave, will have the frequency of

    440 � 25=12 � 587:3 Hz.The strings on a guitar, going from bottom upwards are E, B, G, D, A and

    E. E is referred to as the �rst string, B as the second string and so on.

    String Frequency

    E (1st) 329.6 Hz

    B (2nd) 246.9 Hz

    G (3rd) 196.0 Hz

    D (4th) 146.8 Hz

    A (5th) 110.0 Hz

    E (6th) 82.4 Hz

    Table 1: Fundamental frequencies for the 6 guitar strings.

    Table 1 shows the fundamental frequencies of the 6 guitar strings. It can be

    seen that the �rst string has a frequency four times the sixth string, consequently

    the �rst string is two octaves higher than the sixth.

    Since, frequency changes are less noticeable in higher frequencies and more

    noticeable in lower frequencies, hertz is not always a preferable unit to describe

    di�erences. Instead the unit cent are used, which better corresponds to the

    human ear. The intervals between two adjacent tones are divided into 100

    equally sized cents. This means that a cent is equivalent to approximately 0.25

    Hz at 440 Hz and 0.5 Hz at 880 Hz.

  • 8(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    Figure 2: Fundamental frequency.

    Figure 3: Second overtone.

    2.3 Tones and overtones

    A musical instrument does not produce sound waves of a single frequency. A

    note from a guitar is a superposition of a fundamental frequency and overtones,

    where the overtones are related to the fundamental frequency by integer factor.

    The fundamental frequency is the lowest tone and is vibrating between the end

    points with only one arc, as shown in �gure 2. The �rst and second overtones

    has frequencies twice and three times, compared to the fundamental frequency,

    respectively. The second overtone is shown in �gure 3. Thus, a note from a

    guitar can be modeled by,

    y(t) =

    1X

    k=1

    akcos(2�kf0t) (2.2)

    where f0 is the fundamental frequency and ak is the amplitude of the kth har-

    monics. Figure 4, shows a Fast Fourier Transform (FFT) of a guitar string,

    where the fundamental frequency is found slightly below 200Hz and the over-

    tones at regular intervals by integer factor.

    0 100 200 300 400 500 600 700 800 900 10000

    0.2

    0.4

    0.6

    0.8

    1

    1.2

    1.4

    frequency (Hz)

    Figure 4: FFT of guitar string.

  • 9(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    2.4 Pulse Code Modulation

    One of the most common methods of encoding an analog audio signal into a

    digital form is called pulse-code modulation [4], abbreviated PCM. The signal is

    �rst sampled into a discrete form called pulse-amplitude modulation, or PAM,

    where each sample represents the amplitude of the signal at the time of sampling.

    Each sample is then quantized and encoded into an n-bit digital signal. Figure

    5 illustrate the di�erent stages in the processing of a 4-bit code.

    time

    Am

    plitu

    de

    time

    Am

    plitu

    de

    time

    Am

    plitu

    de

    0011 0100 0100 0011 0001 0001 0010

    a) b) c)

    Figure 5: a) An analog signal, b) The signal represented in PAM, c) The result-

    ing PCM encoded signal.

    2.5 Floating- vs. �xed-point numerics

    On a computer, a fractional number is represented using a pair of integers named

    mantissa (m) and exponent (e). The mantissa carries the numeric information

    of the number, and the exponent contains information about where to place the

    decimal point. The fraction can then be found with the following formula:

    m � 2�e (2.3)

    If the exponent is variable, the number is said to be a �oating-point number.

    With this representation, a wide range of numbers can be represented, from

    the very small to the very large, with very good precision. If the exponent is

    �xed, i.e. known at compile time, it is said to be a �xed-point number. While

    �oating-point numbers o�ers a wider range and better precision, it lacks the

    computational speed that can be achieved when using �xed-point arithmetics.

    Fixed-point numbers are easy to represent, using only an ordinary integer to

    store the mantissa while keeping the exponent in mind when making necessary

    calculations. Assuming that all numbers used will be represented in the same

    format (having the same exponent), the only operations that need any special

    attention are those that may change the exponent of the result. Thus, most

    operations can be used with either no or slight changes to them.

    For instance, performing an addition (or subtraction) does not change the

    exponent and therefore the ordinary operation for addition can be used. A

    general mathematical example follows:

    m1 � 2�e +m2 � 2

    �e = (m1 +m2) � 2�e (2.4)

    However, when multiplying two �xed-point numbers, the exponent of the

    result will di�er from the factors involved in the multiplication.

    m1 � 2�e

    �m2 � 2�e = (m1 �m2) � 2

    �2e (2.5)

  • 10(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    This pose only a slight problem since the result can be rewritten in the

    following manner:

    (m1 �m2) � 2�2e = (m1 �m2 � 2

    �e) � 2�e (2.6)

    Thus, another simple multiplication of the resulting mantissa will make sure

    that the exponent of the result remains the same as the factors involved. This

    leaves us with a normal multiplication followed by an arithmetic shift, a fairly

    simple operation even when the whole is being considered. Division is done in

    a similar fashion. The multiplication (shift), done to convert the result to have

    the same exponent as the factors, has to be performed before the division of the

    mantissas. This is in order to not lose precision.

    Over�ow can be the cause of errors if one does not take heed, so it is impor-

    tant to choose exponents that are suitable for the application being considered.

    To gain more insight on the topic, please refer to [1].

  • 11(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    3 Method

    This section holds a description of the methods used to examine the possibilities

    of implementing a guitar tuner in a mobile. To begin with, algorithms suitable

    for solving the problem is introduced. At the end, an implementation scheme

    for a speci�c mobile phone model is presented.

    3.1 Environment

    Having a noisy environment naturally a�ects the performance of an algorithm

    whose output depends on sound. Even though the in�uence from noise can be

    reduced with the use of �lters, the application may still be susceptible to noise

    and produce an unwanted output. Thus, avoiding an environment with noise,

    especially that which resembles the wanted signal, is encouraged.

    3.2 Frequency-tracking loops

    Frequency tracking loops are frequently utilised in communication systems, e.g.

    in digital data receivers, as frequency acquisition or as carrier frequency control.

    A general frequency-tracking loop, shown in �gure 6, is a composition of a

    frequency di�erence detector (FDD), loop �lter (LF) and a sinusoidal generator.

    The FDD is the fundamental part in the frequency-tracking loop and its appear-

    ance varies depending on the application. The FDD compares the input signal

    and the signal generated by the sinusoidal generator and outputs the frequency

    di�erence. The LF works as a low-pass �lter, averaging out instantaneous phase

    errors in the loop. The sinusoidal generator generates a signal of frequency de-

    pending on the input from the LF. Thus, after a time of operation the error will

    reach zero and the sinusoidal generator will generate a frequency equal to the

    input signal.

    FDD LFInput Signal Error

    Control signalSinegenerator

    Figure 6: General frequency tracking loop.

    In this thesis the frequency-tracking loop will have a rather di�erent area

    of use. The loop will function as a guitar tuner and will have a somewhat

    di�erent appearance. The input signal is represented by the reference signal,

    with wanted frequency. The sinusoidal generator is represented by the guitar

  • 12(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    string and the error produced by FDD is displayed for the user. The user apply

    this information to the guitar by tightening or loosening the guitar strings.

    The following two sections will describe the theory behind two frequencies

    di�erence detectors. The Quadricorrelator, section 3.2.1, and the Balanced

    Quadricorrelator, section 3.2.2. The former provides an output with two com-

    ponents, a useful DC part and an unwanted AC part. The latter provides a

    satisfactory result, with only a DC component. The Quadricorrelator was not

    used because of its weakness. However, with the Quadracorrelator in mind, the

    understanding of the Balanced Quadricorrelator, is straightforward.

    3.2.1 Quadricorrelator

    The Quadricorrelator is shown in �gure 7. It consists of multipliers, low-pass

    �lters and a di�erentiator. The input signal, xin[n], is the sampled signal fromthe guitar string. The two sinusoidal, cos(!0n) and sin(!0n), are generatedsignals, where !0 is the desired radian frequency. Multiplication will produce

    signals with sum and di�erence frequency of the input signal and the generated

    signals. The low-pass �lters will suppress the sum frequency and let the di�er-

    ence frequency pass. Notice that the low-pass �lters will give an approximation

    of the range of operation for the FDD. If the di�erence frequency fall outside the

    passband of the low-pass �lters, the di�erence frequency will also be suppressed.

    However, de�ning a broader passband will allow more noise and signal of higher

    frequencies to pass. The output of one low-pass �lter is di�erentiated to alter

    the phase. Further the outputs are �nally multiplied.

    x

    x LPF

    LPF

    x

    d/dt

    )cos( 0nw

    )sin( 0nw-

    DifferentiatorLow Pass

    FilterMultiplicator

    xin ][n yout ][n

    yI

    yQ

    ][n

    ][n

    Figure 7: Quadricorrelator.

    Below calculations for the Quadricorrelator are presented. For simplicity,

    gains are omitted in the equations, but are included in [2]. Let the input signal

    be:

    xin[n] = cos(!inn+ �in) (3.1)

    where !in and �in is the radian frequency and an arbitrary phase angle, respec-

    tively of the input signal. As stated in section 2.3, tones from guitars are a

    composition of several frequencies. However, section 3.4 will show that, if the

  • 13(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    passband of the low-pass �lters are de�ned correctly, the products generated by

    the overtones will be suppressed. Consequently, the �lter outputs are calculated

    to be:

    yI [n] = cos(�!n+ �in) (3.2)

    yQ[n] = sin(�!n+ �in) (3.3)

    where �! = !in � !0 and !0 is the radian frequency of the generated signal.After di�erention and multiplication, the output of the �nal multiplier is:

    yout[n] = ��!1

    2[1� cos(2�!n+ 2�in)] (3.4)

    The product consists of two parts, one DC part proportional to the frequency

    di�erence, and one AC part with double frequency compared to the frequency

    di�erence. For this thesis, only the DC component is of interest. Thus, an

    algorithm with only this output would be desirable.

    3.2.2 Balanced Quadricorrelator

    In this section the Balanced Quadricorrelator is described, as will be shown,

    this frequency di�erence detector output only consists of a DC part.

    x

    x LPF

    LPF d/dt

    )cos( 0nw

    )sin( 0nw-

    DifferentiatorLow Pass

    FilterMultiplicator

    xin ][n

    d/dt

    +y

    out][n

    x

    x

    +

    -

    yI 2

    ][n

    yQ2 ][n

    yI 1

    ][n

    yQ1 ][n

    Figure 8: Balanced Quadricorrelator.

    The Balanced Quadricorrelator, is shown in �gure 8. One multiplication,

    one di�erention and one subtraction is added to the Quadricorrelator to form

    the Balanced Quadricorrelator. With the Quadricorrelator in mind, the analysis

    of the Balanced Quadricorrelator is straightforward. The output of the two �nal

    multipliers are:

    yI2[n] = ��!1

    2[1� cos(2�!n+ 2�in)] (3.5)

    yQ2[n] = �!1

    2[1 + cos(2�!n+ 2�in)] (3.6)

    Notice the similarities with the Quadricorrelator, in the previous section.

    The output signal of the Quadricorrelator and yI2 are identical. However, the

  • 14(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    Balanced Quadricorrelator also have the signal, yQ2, with identical AC compo-

    nent, but opposite DC sign. Thus, if xin, de�ned in section 3.2.1, is applied to

    the Balanced Quadricorrelator the output is:

    yout = ��! (3.7)

    The AC part has vanished. The AC part of yI2 and yQ2 cancels out each other

    and the DC parts adds together. Furthermore, the phase �in does not a�ect the

    output.

    3.3 Sine wave generation

    To rid the application from a possible bottleneck, occurring when producing

    the sine and cosine values with the functions provided by standard libraries,

    other methods have to be considered. The simplest solution is the use of a

    lookup-table, generating all the necessary values and store them in a simple

    data structure for easy and fast access. Unfortunately, the resources available

    in a mobile phone are quite limited, not only when speed is considered, but also

    resources like RAM and Flash Memory 1. When these constraints are taken

    into account, the lookup-table is no longer an overall ideal solution, and instead

    we will consider the digital sinusoidal oscillator [3]. It is not as fast as the

    lookup-table, but require a very limited amount of Flash Memory.

    z1-

    z1-

    ][ny

    ]1[ -ny

    ]2[ -ny

    a- 1

    a- 2

    +][sin

    0nA dw

    +

    Figure 9: A typical 2nd order IIR �lter that can be used for sine wave generation.

    The digital sinusoidal oscillator is implemented using an IIR �lter. Placing

    the poles on the unit circle of the Argand diagram will make the output oscillate.

    Figure 9 depicts a typical implementation of a 2nd order IIR �lter. This �lter

    has the transfer function:

    H(z) =b0

    1 + a1z�1 + a2z�2(3.8)

    Setting a1 = �2r � cos!o, a2 = 1 and b0 = A � sin!o will place the poles onthe unit circle, and the system will have the unit sample response

    h(n) = A � sin(n+ 1)!0 � u(n) (3.9)

    With this scheme, it is only necessary to calculate the value of sine once with

    the provided function. The necessary values will instead be provided through

    1A form of nonvolatile memory. Sometimes referred to as Flash RAM.

  • 15(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    the use of the digital sinusoidal oscillator, using the �xed-point format instead of

    the slower �oating-point, thus making this part of the application more e�cient.

    3.4 Simulation

    This section describes the simulations made to obtain knowledge about the

    FDDs. The simulations were made in MATLAB, which provides an easy and

    fast way to examine the algorithms. The understanding gained, were applied in

    the implementations of the FDD in the mobile phone. The �rst part discusses

    the Quadricorrelator and examines the output from this FDD. Next, the Bal-

    anced Quadricorrelator are discussed with �oating-point precision. Both ideal

    environment and a real environment with noise and overtones are covered. Fi-

    nally, the Balanced Quadricorrelator are discussed with �xed-point precision.

    Also here, both ideal and real environments are covered.

    0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5−1

    −0.8

    −0.6

    −0.4

    −0.2

    0

    0.2

    0.4

    0.6

    0.8

    1x 10

    −3

    Time (s)

    Am

    plitu

    de

    0 10 20 30 40 50 60 70 80 90 1000

    0.5

    1

    1.5

    2

    2.5

    3

    3.5

    4

    4.5

    5x 10

    −4

    frequency (Hz)

    Am

    plitu

    de

    Figure 10: Output from the Quadricorrelator, with 440 Hz as reference fre-

    quency and 445 Hz sinusoidal input.

    Figure 10, shows the output of the Quadricorrelator, with reference fre-

    quency set to 440 Hz and a 445 Hz sinusoidal input. The plots illustrate the

    outputs in the time domain (left plot) and the frequency domain (right plot),

    respectively. During these simulations no noise or overtones were applied. As

    stated in section 3.2.1, the output has a negativ DC level, depending on the

    frequency di�erence and an AC signal, with 2�! frequency. The AC part isundesirable for a guitar tuner. Thus, a FDD without an AC part would be

    preferable.

    The Balanced Quadricorrelator, described in section 3.2.2, does not contain

    an AC signal. The output, shown in �gure 11, should only consist of a DC

    signal. However, due to non-ideal operators some ripple appears. The reason

    for this is, that the low-pass �lters do not suppress higher frequency su�ciently

    and the di�rentiators do not operate perfectly. The conditions for this plot are

    ideal, no noise and no overtones are present.

    Interesting parts of �gure 11, are the unwanted ripple and the time in which

    the system stabilises to a certain level. A low degree of ripple is desirable to

    obtain accuracy and a faster system is desirable to acquire faster response time

    during tuning. Further, the working area of the FDD is of interest. That is,

    which certain level of di�erence the FDD loses the possibility to detect the

    di�erence. To obtain an FDD, which have suitable properties in these �elds,

  • 16(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    0 0.05 0.1 0.15 0.2−1

    −0.8

    −0.6

    −0.4

    −0.2

    0

    0.2

    0.4

    0.6

    0.8

    1x 10

    −3

    Time (s)

    Am

    plitu

    de

    Figure 11: Output from the Balanced Quadricorrelator, with 440 Hz as reference

    frequency and 445 Hz sinusoidal input.

    di�erent low-pass �lters in the Balanced Quadricorrelator are examined. Simu-

    lations in both ideal and non-ideal conditions are made. First, in �oating-point

    precision, to acquire knowledge about the FDDs behaviour in general. Later,

    in �xed-point precision, to acquire knowledge about its behaviour in the mobile

    phone, speci�cally.

    3.4.1 Floating-point precision

    This section only consider the balanced quadricorrelator since the non balanced

    version was never part of the implementation.

    0 0.05 0.1 0.15 0.2 0.25−10

    −9

    −8

    −7

    −6

    −5

    −4

    −3

    −2

    −1

    0

    1x 10

    −4

    Am

    plitu

    de

    Time (s)0 0.05 0.1 0.15 0.2 0.25

    −10

    −8

    −6

    −4

    −2

    0

    2

    x 10−4

    Time (s)

    Am

    plitu

    de

    Figure 12: Balanced Quadricorrelator performance in non-noisy and noisy en-

    vironments, with the low-pass �lters set to 5 Hz cut-o� frequency. The input

    signal frequencies range from 440 Hz to 445 Hz, with 1 Hz incrementation, and

    the reference frequency set to 440 Hz. Notice that the di�erent bias levels on the

    output corresponds to the frequency di�erences between the input and reference

    signals.

  • 17(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    Figure 12 to 14, show the performance of the Balanced Quadricorrelator in

    ideal and non-ideal environments, with di�erent �lter parameter settings. The

    �rst plot in each �gure shows the performance in ideal environments. In the

    second plot, noise and 10 overtones are added to the insignal to simulate a

    realistic input.

    In �gure 12, low-pass �lters with cut-o� frequencies at 5 Hz are used. The

    low-pass �lters in �gures 13 and 14 utilise 10 Hz and 20 Hz cut-o� frequency,

    respectively.

    The plots in �gure 12, illustrates the performance of the FDD in non-noisy

    and noisy environments, with 5 Hz as cut-o� frequency. The impact from noise

    and overtones are obvious. The output is more unstable, than the non-noisy

    plot. In addition, more ripple is introduced, in the plot.

    0 0.05 0.1 0.15 0.2 0.25

    −10

    −8

    −6

    −4

    −2

    0

    x 10−4

    Am

    plitu

    de

    Time (s)0 0.05 0.1 0.15 0.2 0.25

    −10

    −8

    −6

    −4

    −2

    0

    2

    x 10−4

    Am

    plitu

    de

    Time (s)

    Figure 13: Balanced Quadricorrelator performance in non-noisy and noisy en-

    vironments, with the low-pass �lters set to 10 Hz cut-o� frequency. The input

    signal frequencies range from 440 Hz to 445 Hz, with 1 Hz incrementation, and

    the reference frequency set to 440 Hz. Notice that the di�erent bias levels on the

    output corresponds to the frequency di�erences between the input and reference

    signals.

    The plots in �gure 13, illustrates the performance of the FDD, with 10 Hz

    cut-o� frequency. Compare the non-noisy plots in �gures 12 and 13. Notice,

    that the time for the FDD to stabilise on a given level is shorter. Thus, a higher

    cut-o� frequency gives a faster FDD. However, in noisy environment the FDD,

    with 10 Hz cut-o� frequency, is �uctuating more than in non-noisy environment.

    The plots in �gure 14, illustrates the performance of the FDD, with 20 Hz

    cut-o� frequency. The non-noisy plot stabilises faster to a given level. The noisy

    plot, however, is �uctuating even more.

    Hence, with a faster FDD, accuracy is lost. This is, with lower cut-o�

    frequency in �lters, higher frequency signals and noise are suppressed and the

    output varies less. However, the inertia in the low-pass �lters results in inertia

    in the FDD. And higher cut-o� frequency in �lters, results in lower inertia,

    but also a more �uctuating output, when more noise and higher frequencies are

    allowed in the passband.

  • 18(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    0 0.05 0.1 0.15 0.2 0.25

    −10

    −8

    −6

    −4

    −2

    0

    x 10−4

    Time (s)

    Am

    plitu

    de

    0 0.05 0.1 0.15 0.2 0.25

    −10

    −8

    −6

    −4

    −2

    0

    2

    x 10−4

    Time (s)

    Am

    plitu

    de

    Figure 14: Balanced Quadricorrelator performance in non-noisy and noisy en-

    vironments, with the low-pass �lters set to 20 Hz cut-o� frequency. The input

    signal frequencies range from 440 Hz to 445 Hz, with 1 Hz incrementation, and

    the reference frequency set to 440 Hz. Notice that the di�erent bias levels on the

    output corresponds to the frequency di�erences between the input and reference

    signals.

    3.4.2 Fixed-point precision

    Generally, �oating-point calculations, are relatively slow. Thus, often �xed-

    point calculations are used, when processing large amount of data. Although

    precision are lost, �xed-point are preferrable, due to speed gained during calcu-

    lations. Fixed-point numerics are described in section 2.5.

    0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5

    −1.4

    −1.2

    −1

    −0.8

    −0.6

    −0.4

    −0.2

    0

    0.2

    Time (s)

    Am

    plitu

    de

    0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5

    −1.4

    −1.2

    −1

    −0.8

    −0.6

    −0.4

    −0.2

    0

    0.2

    Time (s)

    Am

    plitu

    de

    Figure 15: Balanced Quadricorrelator performance in non-noisy and noisy en-

    vironments, with the low-pass �lters set to 5 Hz cut-o� frequency. The input

    signal frequencies range from 440 Hz to 445 Hz, with 1 Hz incrementation, and

    the reference frequency set to 440 Hz. Notice that the di�erent bias levels on the

    output corresponds to the frequency di�erences between the input and reference

    signals.

    This section describes simulations with �xed-point, done to acquire knowl-

    edge about the algorithms performance in a mobile phone. Figure 15 to 17,

    show the performance of the Balanced Quadricorrelator in ideal and non ideal

    environments, with di�erent �lter parameter settings. The �rst plot in each

  • 19(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    �gure shows the performance in ideal environments. In the second plot, noise

    and 10 overtones are added to the insignal to simulate a realistic input.

    0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5

    −0.25

    −0.2

    −0.15

    −0.1

    −0.05

    0

    0.05

    Time (s)

    Am

    plitu

    de

    0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5

    −0.25

    −0.2

    −0.15

    −0.1

    −0.05

    0

    0.05

    Figure 16: Balanced Quadricorrelator performance in non-noisy and noisy en-

    vironments, with the low-pass �lters set to 10 Hz cut-o� frequency. The input

    signal frequencies range from 440 Hz to 445 Hz, with 1 Hz incrementation, and

    the reference frequency set to 440 Hz. Notice that the di�erent bias levels on the

    output corresponds to the frequency di�erences between the input and reference

    signals.

    During �xed-point simulations, intense ripple are introduced caused by the

    inferior precision. Hence, an additional low-pass �lter is appended to deal with

    this problem. This solution slows down the FDD, but is needed to obtain

    acceptable output.

    Figure 15, shows the performance of the FDD with 5 Hz cut-o� frequency

    in the low-pass �lters. This system is unstable and starts to oscillate in both

    environments. No e�orts were made to analyse this.

    0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5−0.4

    −0.35

    −0.3

    −0.25

    −0.2

    −0.15

    −0.1

    −0.05

    0

    0.05

    Time (s)

    Am

    plitu

    de

    0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5−0.4

    −0.35

    −0.3

    −0.25

    −0.2

    −0.15

    −0.1

    −0.05

    0

    0.05

    Figure 17: Balanced Quadricorrelator performance in non-noisy and noisy en-

    vironments, with the low-pass �lters set to 20 Hz cut-o� frequency. The input

    signal frequencies range from 440 Hz to 445 Hz, with 1 Hz incrementation, and

    the reference frequency set to 440 Hz. Notice that the di�erent bias levels on the

    output corresponds to the frequency di�erences between the input and reference

    signals.

    Figure 16, shows the performance of the FDD with 10 Hz cut-o� frequency

  • 20(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    in low-pass �lters. The plot has angularities, due to �xed-point precision. The

    di�erence between the two plots are small, except from the initial phase. The

    plot with noise has an incorrect minor peak in the beginning, caused by the

    overtones. However, �nally it gets similar behaviour to the non-noise plot.

    Figure 17, shows the performance of the FDD with 20 Hz cut-o� frequency in

    low-pass �lters. The initial peak in this plot does not have the same magnitude

    as the previous plot.

    The above simulations show that, di�erences of 1 Hz between reference fre-

    quency and input frequency can be detected, theoretically. The �nal low-pass

    �lter, added to deal with the ripple, has a negative impact on the performance

    of the FDD, by slowing down the operation. Nevertheless, the �lter is needed

    to obtain an acceptable output quality with �xed-point calculations.

    Further empirical analysis of the algorithm were made in the mobile phone

    to obtain knowledge of performance in real-life conditions. These were done, by

    basically tuning a guitar with di�erent �lter parameters.

    3.5 Implementation

    This section describe the implementation derived from the methods discussed in

    previous sections. Whereas the methods are general and suitable for any system,

    the implementation described here will be aimed at a speci�c platform, namely

    Sony Ericsson's T68. The goal has been to utilise as much of the resources

    available, to shorten delays between updates of the display, without tampering

    with the ordinary functionality of the mobile phone.

    The C programming language has consistently been used for the implemen-

    tation, even though the possibilities of assembly programming has been consid-

    ered.

    Figure 18: The user interface of the tuner application.

  • 21(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    User interface

    When the tuner application has been selected, the display will present the graph-

    ical user interface (GUI), found in �gure 18, to the user. The joystick is used

    for selecting the tone the user wish to tune, and the one currently selected is

    displayed at the bottom of the display. When a string is being plucked, the

    hand (in the future referred to as tone-hand) will indicate whether the tone is of

    higher, lower or the same frequency as the selected tone. Thus, the tone-hand

    tells the user if the string needs to be tightened or loosened. Unless the string

    needs no adjustment, the procedure is repeated until the tone-hand indicate

    that the string has been tuned.

    How it works

    This particular model of mobile phone has two processors available, one AVR2

    that can be seen as the main processor, and one ARM7 3 that in this application

    is used as a co-processor. The workload of processing the samples is divided

    between the two processors. The AVR-processor deals with sampling, precalcu-

    lations of the samples and updating the GUI, while the ARM processor is used

    for most of the signal processing. Figure 19 illustrate the implementation in

    general, while �gure 20 hold a detailed view of the processes involved.

    AVRARMDISPLAY

    - +

    Microphone

    1

    24

    3

    Figure 19: An overview of the implementation scheme.

    The sampling process, located in the AVR and driven by interrupts, is re-

    sponsible for fetching and storing blocks of samples. The microphone interface

    deliver 16-bits PCM-samples, and a shared bu�er is used to forward the data

    to the sending process. Data in the bu�er is protected from being prematurely

    read or overwritten, i.e. overwritten before it has been read, using a counting

    semaphore. When data is available in the bu�er, the sending process is sig-

    naled and allowed to read a block of data, normalize the samples, and send it to

    the ARM. The tuning process perform all necessary calculations (section 3.2.2)

    2AVR is a microprocessor built by Atmel. For more information, please consult their

    webpage at http://www.atmel.com3The ARM processors are developed by Advanced Risc Machines. More information about

    the processors can be found at http://www.arm.com

  • 22(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    needed to decide the deviation from the reference frequency, i.e. the frequency

    of the tone looked after, and then sends the result back to the AVR. When re-

    ceiving a result from the ARM, the MMI process updates the display to notify

    the user about possible changes.

    SamplingProcess

    SendingProcess

    Sample Buffer

    MMIProcess

    Data

    TuningProcess

    AVR ARM

    Figure 20: Schematic view of the processes involved.

    Volume threshold

    To make the application less susceptible to update the display when noise is the

    only sound being received, a volume threshold is set. Hence, unless the sample

    volume reach the speci�ed threshold, no samples will be sent to the ARM and

    no update of the display is made.

    Hardware limitations

    One of the hardware limitations is that �lters will suppress all frequencies below

    300 Hz and above 3.4 kHz. Since almost all of the strings on a tuned guitar

    produce tones with a lower frequency than 300 Hz, this pose a serious limitation

    to the application. However, with the use of overtones (section 2.3), it is still

    possible to tune all the strings on the guitar.

    Another limitation is the processing power. Even though the use of �xed-

    point arithmetics (section 2.5) remedy this problem partially, it is still not pos-

    sible to process all the incoming samples. Instead, a subset of the samples are

    used in order to produce a smooth and continuos updating scheme for the tone-

    hand. The bu�er, seen in �gure 20, is used to get as long continuos signal as

    possible, something that is desirable when trying to decide the current tone.

  • 23(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    4 Result and discussion

    The accuracy of the tuner is quite di�cult to determine, but with the help of

    a PC tuner application4 and a commercial tuning device5, an estimation has

    been made. On average, the deviation from the desired tone is less than +�3 cent

    (for the de�nition of cent, see section 2.2). This is comparable to the guitar

    tuners that emerged early on the market, but a musician with a good ear would

    probably be able to notice the deviation.

    Currently, the implementation is unable to handle incoming calls. Due to

    lack of time, this shortcoming was given low priority and was never really looked

    into. However, only minor modi�cations are probably needed to be able to

    handle this. Even though the speci�cation for the thesis clearly stated that this

    was not a requirement, it would still have been desirable to handle calls.

    4AP Guitar Tuner 1.02 by Audio Phonics.5Matrix SR-4000 Auto Chromatic Tuner.

  • 24(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    5 Conclusions

    The hardware suppresses frequencies outside 300 Hz and 3.4 kHz, so frequencies

    below 300 Hz can not be used. It would be preferable with hardware that does

    not suppress lower frequencies though a majority of the fundamental frequencies

    on a guitar are below 300 Hz. However, it is possible to use the overtones of the

    strings.

    The implementation is not as robust and easy to use as is desired. So ad-

    ditional work is needed for the tuner to become a commercial product. This is

    discussed further in section 6.

    More processing power are needed. If the application where implemented

    in the T68, the DSP would be a preferable place to implement the algorithm.

    However, future phones will have better processors and better processing power.

    Thus, an ordinary processor might be su�cient in the future.

  • 25(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    6 Future work

    Although, a guitar can be tuned with existing implementation, it needs some

    further studies to become a commercial product.

    The tuner must be more robust. Current implementation gives a rather

    uncertain impression. Further analysis of the FDD should be made to improve

    this. Other algorithms, should also be considered, for example, phase-locked

    loops.

    Additional, performance when the string is out of range needs improvements.

    As previously described, the tuner does not give an accurate answer if the devi-

    ation is too large. A commercial product is required to have better out of range

    performance, as the user always should be informed if the string should to be

    loosened or tightened. The tuner should also be able to identify an arbitrary

    frequency. That is, the users does not have to select tone. The tuner identi�es

    the frequency and associates it, with the closest tone. Both these features, could

    probably be solved with FFT.

    In this thesis, no e�orts where made to examine the DSP, existing in the

    mobile phone. The DSP is however of high interest for implementing the algo-

    rithms. If further e�orts are made, resources should be allocated to study the

    DSP.

    Currently, the tuner is only able to tune guitars. Tuning others instruments,

    like violins and wind instrument is a possible continuation.

  • 26(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    A Glossary

    AC Alternating Current.

    DC Direct Current.

    DSP Digital Signal Processor.

    FDD Frequency Di�erence Detector.

    FFT Fast Fourier Transform.

    GUI Graphical User Interface.

    IIR In�nite Impulse Response.

    LF Loop Filter.

    LPF Low Pass Filter.

    MMI Man Machine Interface

    PAM Pulse Amplitude Modulation.

    PCM Pulse Code Modulation.

    RAM Random Access Memory.

  • 27(27)

    Open

    REPORT

    Prepared (also subject responsible if other) No.

    LD/ECS/GUB/DK Henrik Ahlm / Mathias Jeppsson

    Approved Checked Date Rev Reference

    LD/ECS/GUB/DK (John Sarhede) 2002-06-05 A

    References

    [1] Advanced RISC Machines Limited, www.arm.com. Application Note 33,

    Fixed Point Arithmetic on the ARM, September 1996.

    [2] Gardner Floyd M. Characteristics of frequency-tracking loops. In Chak

    M. Chie William C. Lindsey, editor, Phase-Locked Loops. IEEE Press, 1985.

    [3] Dimitris G. Manolakis John G. Proakis. DIGITAL SIGNAL PROCESS-

    ING : Principles, Algorithms and Applications, chapter 4, pages 352�354.

    Prentice-Hall, 3rd edition, 1996.

    [4] Ken C. Pohlmann. Principles of Digital Audio, chapter 3, pages 49�52.

    McGraw-Hill, 4th edition, 2000.