Design of a Guitar Tab Player in MATLAB Background Lecture Module 1: Modeling a Guitar Signal
Multitude of guitar tabs on the Internet Some are good some are not The Problem Multitude of guitar tabs on the Internet Some are good some are not Wasted time and frustration for an amateur guitar player trying to learn a song Having a way to check quickly if a guitar tab is good or not
Typical Electric Guitar Low E D frets g b High e to amplifier magnetic pickups A typical electric guitar has 6 strings and 24 frets
e|------------------------1-0----------| Guitar Tablature e|------------------------1-0----------| B|----------------------------1-3p1-0--| G|-------------------------------------| D|------------------3-2-0--------------| A|------------3-2-0--------------------| E|--0-0-3-2-0--------------------------| Guitar tablature is a form of musical notation that is well suited for playing the guitar. It shows the six strings of the guitar. The number and its location corresponds to the string to pluck and the fret where to place the finger. Fret 0 represents the case where one leaves the string open
Playing the Guitar Tablature B|----------------------------1-3p1-0--| G|-------------------------------------| D|------------------3-2-0--------------| A|------------3-2-0--------------------| E|--0-0-3-2-0--------------------------| Low E fret 2 Low E (open string) Low E (open string) Low E fret 3 Low E A D G B High e
Guitars and Guitar Signals What Do We Know About Guitars and Guitar Signals
Guitar String Vibrations First Harmonic (Fundamental) Second Harmonic Third Harmonic The string must make an integer number of half sines between the two points it is attached too
Fundamental Frequency for a String f0 is the frequency of the sound signal in Hertz (Hz) m is the linear mass of the string given in kilograms per meter (kg/m) L is the length of the string in meters (m) T is the tension on the string giving in Newtons (N) A guitar string is tuned by tightening or loosening the string (increasing or decreasing T) The frequencies produced by a bass are lower than that of a guitar. This is why a bass has longer strings than a guitar (greater L value).
Fundamental Frequencies for a Guitar String Fundamental Frequency Low E 82 Hz A 110 Hz D 147 Hz G 196 Hz B 247 Hz High e 330 Hz The guitar will be tuned when the strings produce the fundamental frequencies above. (The fundamental frequencies for a bass are 41, 55, 73, and 98Hz)
Fundamental Frequencies with Frets fret 3 fret 2 fret 1 When placing the finger on a fret, the sound has a higher pitch (higher frequency) than that of the open string. The frets are designed such that the fundamental frequencies produced are: where f0 string = 82, 110, 147, 196, 247, or 330Hz depending on the string. Example: Plucking the Low E string with the finger on the second fret produces a sound with fundamental frequency:
How Can the Guitar Signal Be Modeled?
Mathematical Formulation: Pure Tone or Sinewave A T0 Fundamental Period t -A Mathematical Formulation:
Single Sinewave Model String Guitar Recording Low E 82 Hz A 110 Hz D 147 Hz G 196 Hz B 247 Hz High e 330 Hz Modeling the sound of a guitar string using a single sinewave does not sound satisfactory. It seems that the guitar string has a higher “pitch” than the single sinewave.
Adding Several Sinewaves (Fundamental and Harmonics)
Signal Obtained from Adding Sinewaves
Issues How many harmonics to use? Are the amplitudes all equal to 1? Does the signal obtained look like the actual signal? More importantly: Does the signal obtained sound like the actual signal? Take measurements to check/improve the model
Measuring Actual Guitar Signals Using a Computer Sound Card guitar output jack plug PC/laptop microphone or line input jack connector
Sampling in an Analog Signal (MIC/Line Input): Analog to Digital Conversion Sampling Theorem: One must sample at least twice as fast as the highest frequency contained in the signal
Producing an Analog Signal (Speaker Output): Digital to Analog Conversion
Sound Card Control (Volume Control - Playback) D/A Control Programs->Accessories->Entertainment->Volume Control Typical sound cards can produce electrical signals up to +/- 2 volts maximum. This explains why one needs external speakers with power supply for louder sounds.
Sound Card Control (Volume Control - Recording) A/D Control Signals at the MIC or line input should not exceed +/- 150 mV
Microsoft Windows Sound Recorder Programs->Accessories->Entertainment->Sound Recorder Sound signals are most commonly stored into .wav files. The samples are obtained using Pulse Code Modulation (PCM). One has the flexibility to specify the sampling frequency and the number of bits
Measurement of the Open Low E String
Three Sinewaves Model actual recording model
Issues It sounds better than the single sinewave model but it is difficult to tell from the time waveform: How many harmonics to use The amplitudes of the sinewaves to use If the frequencies of the sinewaves are as expected The Fourier Transform can be used to show the frequencies and amplitudes contained in the signal. The results are shown in a what is called the Frequency Spectrum graph
Frequency Spectrum of Harmonic Signals Single sinewave (single tone) Frequency Spectrum Math Formula Several sinewaves (fundamental and harmonics) Frequency Spectrum Math Formula
Measured Frequency Spectrum of the Open Low E String f = 82 Hz, amplitude = 0.7251 f = 164 Hz, amplitude = 0.9789 f = 246 Hz, amplitude = 1.0000 f = 328 Hz, amplitude = 0.4764 fundamental (82 Hertz) second harmonic (164 Hertz) third harmonic (246 Hertz) forth harmonic (328 Hertz) fifth harmonic (410 Hertz) becoming negligible The measured frequency spectrum is obtained by running a Fast Fourier Transform (FFT) program on the time samples of the guitar string. One can clearly see the fundamental and harmonics, their frequencies and amplitudes.
Other Measured Frequency Spectrums Frequency spectrum of a human voice (somewhat harmonic) Frequency spectrum of a drum type sound (not harmonic)
Measurement Based Model
Mathematical Model of an Open String The guitar signal is modeled as a sum of several sinewaves (fundamental and harmonics): The fundamental frequency f0 depends on the string and is given for a tuned guitar by: String f0 Low E 82 Hz A 110 Hz D 147 Hz G 196 Hz B 247 Hz High E 330 Hz The amplitudes A1, A2, A3, etc of the sinewaves in the model are obtained by running the FFT program on the measured time samples of the open string as shown in the next slide.
Getting the Amplitudes A1, A2, A3, etc for the Model Run the FFT program on the measurement .wav file of the open string. Then locate the peaks and report the fundamental/harmonics amplitudes A1, A2, A3, A4, etc using the Frequency Spectrum graph produced. Check that depending on the string, f0 = 82, 110, 147, 196, 247 or 330Hz.
Modeling any String/Fret Combination The model can be extended to any of the string/fret combinations. The main difference comes from the fundamental frequency to use. For example calling fx the fundamental frequency for a string with a given fret gives the general model: Note that the general model also works for open strings since String f0 Low E 82 Hz A 110 Hz D 147 Hz G 196 Hz B 247 Hz High E 330 Hz
Design Choices To complete the general model, one needs to define the sinewave amplitudes A1, A2, A3, etc. There are two straightforward methods: Take samples of all 144 string/fret combinations and extract the sinewave amplitudes from the measured frequency spectrums obtained running the FFT program. Reuse the same sinewave amplitudes as those obtained for the open strings. Pros/Cons: Solution 1 is more accurate than solution 2 but will require a lot more work and a lengthier program. Solution 2 should be sufficient for the application: playing quickly guitar tabs to check if the tab is valid or not.
Effects of Decay The general model does not go down after some time as does the actual guitar string signal. We need to add something in the model that can make the signal go down over time.
Exponential Decay The previous model is multiplied by an exponential function so that its amplitude will decrease over time: The exponential function starts at 1 when t = 0 and drops to 0.37 when t = T. Therefore to estimate T, one checks to see when the time waveform has dropped from 1 to 0.37. T about 1.8 seconds 0.37
Some Useful MATLAB Functions
Reading a .WAV File and Plotting the Waveform clc; clear; close all; filename = input('wav file name => ','s'); % Enter file name from keyboard [vt, Fs, nbits] = wavread(filename); % vt has the amplitude samples % Fs has the sampling rate Ts = 1/Fs; % Compute sampling time Ts N = length(vt); % N is the number of samples t = Ts*[0:1:N-1]; % Form times values using Ts figure(1) % Plot vt versus t plot(t, vt) grid on title('v(t)')
Generating a Sinewave and Saving It in a .WAV File clc; clear; close all; Fs = 8000; % Set the sampling rate Fs Ts = 1/Fs; % Compute sampling time Ts nbits = 16; % Set the number of bits nbits N = 800; % Set the number of points N t = Ts*[0:1:N-1]; % Form the time values f0 = 10; % Set the sinewave frequency f0 vt = 2*sin(2*pi*f0*t); % Generate the sinewave filename = input('wav file name => ','s'); % Get the filename from keyboard wavwrite(vt, Fs, nbits, filename); % Save the sinewave vt in the .wav file
Clipping Issues Clipping One should scale the amplitude of the waveform to fit between +/- 1 before saving it to the .wav file. If not the waveform will be “clipped” as shown below. Clipping
Fixing the Clipping Issue clc; clear; close all; Fs = 8000; Ts = 1/Fs; nbits = 16; N = 800; t = Ts*[0:1:N-1]; f0 = 10; vt = 2*sin(2*pi*f0*t); vt = vt/max(vt); % Divide vt by % the maximum value in vt filename = input('wav file name => ','s'); wavwrite(vt, Fs, nbits, filename);
Fast Fourier Transform (FFT) The MATLAB function called fft.m can be used to compute the frequency spectrum of a signal when the time waveform samples are provided. filename = input('wav file name => ','s'); [vt, Fs, nbits] = wavread(filename); Ts = 1/Fs; N = length(vt); df = Fs/N; t = Ts*[0:1:N-1]; f = df*[0:1:N-1]; % Create appropriate frequency values Vf = fft(vt); % Computes the FT values (complex) Vfnorm = Vf/max(abs(Vf)); % Normalize to +/- 1 figure(1) plot(f, abs(Vfnorm), 'k') % Plot the magnitude versus frequency axis([0 Fs/2 0 1]) % Limit plot from 0 to Fs/2 (x-axis) grid on % and from 0 to 1 (y-axis) legend('|Vnorm(f)|') xlabel('f (Hz)')
Playing Sounds To play sounds directly in MATLAB, one can use the functions sound.m, soundsc.m, or wavplay.m SOUND(Y,FS) sends the signal in vector Y (with sample frequency FS) out to the speaker. Values in Y are assumed to be in the range -1.0 <= y <= 1.0. Values outside that range are clipped. Stereo sounds are played, on platforms that support it, when Y is an N-by-2 matrix. SOUNDSC(Y,...) is the same as SOUND(Y,...) except the data is scaled so that the sound is played as loud as possible without clipping. WAVPLAY(Y,FS) sends the signal in vector Y with sample frequency of FS Hertz to the Windows WAVE audio device. Standard audio rates are 8000, 11025, 22050, and 44100 Hz. For stereo playback, Y should be an N-by-2 matrix. Values in Y are assumed to be in the range -1.0 <= y <= 1.0. Values outside that range are clipped.
Example of Playing Sounds clc clear close all Fs = 8000; % Sampling rate Fs Ts = 1/Fs; % Compute sampling time Ts N = 8000; % 8000 pts last 1 second t = Ts*[0:1:N-1]; % Form t vector with values f0 = 100; % separated by Ts seconds vt = 1*sin(2*pi*f0*t);% Compute the samples of % the sinewave sound(vt, Fs) % Send it to the sound card
Example of Playing Stereo Sounds Fs = 8000; Ts = 1/Fs; N = 8000; t = Ts*[0:1:N-1]; f1 = 100; f2 = 500; vleft = 1*sin(2*pi*f1*t); vright = 1*sin(2*pi*f2*t); vt = [vleft' vright']; % vt has two columns % vleft and vright sound(vt, Fs)