# Electronic Music Analysis and Synthesis_SOLUTION

Files necessary to compute this assignment: yankee.m, yankee harmonics.m, yankee amplitude.m, beethoven.mat, bach.wavDeliverables Due: Before your assigned lab session between Oct. 6th – Oct. 10th
Introduction
In signal processing, two key concepts arise continuously: analysis and synthesis. Analysis is the act of decomposing a signal into an alternative representation. Synthesis is the act of building a signal from an alternative representation. We use signal representations to better understand, manipulate, and generate data. In this lab assignment, we will focus on one very important representation of signals: the Fourier (or frequency) representation. In fact, the study of this representation is the topic of most of ECE 3500. This lab is intended to give you a small taste of what is to come.
Throughout this assignment, we will be analyzing and synthesizing musical notes. Musical notes are arranged in groups of twelve, called octaves. The fundamental frequencies associated with the twelve notes in the octave of frequencies ranging from 220 Hz to 440 Hz. We will be using a 12-tone equal-temperament note scale (i.e., the standard, modern music scale). The frequency 220 Hz is known as the note ‘A’. Each following note has a frequency of 220×2(n/12), where n is the number of notes higher than ‘A’ (‘A’, ‘A#’, ‘B’, ‘C’, ‘C#’, ‘D’, ‘D#’, ‘E’, ‘F’, ‘F#’, ‘G’, ‘G#’). The next ‘A’ note then starts at a frequency of 440 Hz.
This lab assignment has three learning objectives:
1. To familiarize yourself with Fourier analysis in MATLAB
2. To analyze a simple music piece (in this case, Yankee Doodle) using Fourier analysis
3. To synthesize musical pieces (in this case, music from Beethoven and Bach)
Your experiment will be divided into three parts, each associated with an individual objective. In your lab report, you address each part in separate subsections. See the lab report guidelines on the class website for more information.
Deliverables
This lab assignment has three deliverables:
1. One (1) lab report (see the “Lab Report Guidelines” on how to write it)
2. One (1) copy of your custom, ﬁnal, computer-synthesized “.wav” ﬁle of beethoven
3. One (1) copy of your custom, ﬁnal, computer-synthesized “.wav” ﬁle of bach
Please submit a hard copy of the paper deliverables and e-mail the electronic deliverables to your lab teaching assistant before your assigned lab session between Oct. 7th – Oct. 13th.

Part 1: Basics Fourier Analysis
In this lab assignment, we will use the Fourier transform. However, we know very little about the Fourier Transform right now. Therefore, we will treat the Fourier transform as a black box for now. What you need to know for now is: (1) the input for the Fourier Transform is a time signal and (2) the output for the Fourier Transform is the frequency representation of that time signal. That is, if we imagine our signal as a sum of many diﬀerent cosines and/or sines at diﬀerent frequencies, the frequency representation tells us what those frequencies are.
Run a Simple Experiment: To demonstrate Fourier analysis, go into MATLAB and run the following code.
t = [0:1000]/8000; % Discrete time instants. f = 1000; % Frequency. x = sin(2*pi*f*t); % Generating a sine wave. fs = 8000; % Sampling frequency. Npoints = 2*length(x); % Number of frequency points which % Fourier transform is performed % on. It is arbitrarily chosen to % be twice the length of the signal vector [H,F] = freqz(x,1,Npoints,fs); % Perform Fourier transform. plot(F,abs(H)); % Plot magnitude of the Fourier transform. xlabel(’Frequency [Hz]’); % Label the x-axis ylabel(’Magnitude’); % Label the y-axis (do not worry about units right now) title(’Frequency representation’); % Plot title
This code computes the absolute value of the frequency representation of our time signal, a simple sine wave with a frequency of 1000 Hz. This process is known as Fourier analysis. We are using the Fourier Transform to decompose our time signals into a frequency (or Fourier) representation.
Write a MATLAB Function: We will be using Fourier analysis a lot in the rest of this lab assignment. Since we do not want to rewrite the above code constantly, we should write it as a MATLAB function
function [H,F]=f_analysis(x,fs) \; .
The function takes the signal vector x and the sampling frequency fs and generates a plot of the magnitude of Fourier transform of x. Your function should generate the output parameters F, a vector that contains frequency points, and H, the magnitude of the frequency representation of x.
Analyze a New Signal: Use the function that you have developed to analyze the signals x1 and x2, which are generated as follows, and identify their spectral contents. By doing this, you are able to conﬁrm the correctness of your function f analysis.
t = [0:1000]/8000; f1 = 500; f2 = 1000; f3=2000; f4 = 3000; x1 = 2*sin(2*pi*f1*t)+0.5*cos(2*pi*f3*t)-0.25*cos(2*pi*f4*t); x2 = 4*cos(2*pi*f2*t)-1.5*sin(2*pi*f3*t)+1.5*cos(2*pi*f4*t);

Include in Lab Report “Results and Discussion” Section:
Include: The time-domain plots of x1 and x2.
Include: The frequency representation plots of x1 and x2.
Answer: What does the frequency response represent? How is this shown in the frequency representations of x1 and x2?
Answer: Why is this representation useful? How might we use it in practice?

Part 2: Music Analysis
In this part of the lab assignment, we utilize our new knowledge and new MATLAB function f analysis from Part 1. We use this knowledge to analyze a simple song: Yankee Doodle. We will analyze Yankee Doodle as synthesized with diﬀerent types of notes: (1) pure sinusoidal notes, (2) notes with additional harmonics, (3) notes with amplitude variations. The amplitude variations deﬁne the “attack” and “decay” of the notes.
Analyze Yankee Doodle: Generate a vector called music by running music = yankee. The yankee function can be downloaded on the course website. Listen to the music (using sound(music, 8000)) and plot the music in the time domain (using plot(music)). Observe that it consists of individual notes.
Identify the position of diﬀerent notes in diﬀerent segments of the music. For example, the ﬁrst few hundred elements of music contain the note “c” (around 261.6256 Hz). Zoom in to a level such that you can see the individual waveforms.
Extract individual notes from music and use your f analysis function to determine the frequency of each note. Compare these frequencies with the programmed music notes.
Hint: Although not required, an automated (or semi-automated) function that can extract and analyze each note may make the rest of this assignment easier.
Analyze Yankee Doodle with Harmonics: Generate a vector music harmonics by running music harmonics = yankee harmonics. The function yankee harmonics can be downloaded on the course website. Listen to the music and plot the music in the time domain.
Repeat the steps used for the normal Yankee Doodle (extract all of the notes and determine the frequency of each note).
Analyze Yankee Doodle with Amplitude Variations: Generate a vector music amplitude by running music harmonics = yankee amplitude. The function yankee amplitude can be downloaded on the course website. Listen to the music and plot the music in the time domain.
Repeat the steps used for the normal Yankee Doodle (extract all of the notes and determine the frequency of each note).
Include in Lab Report “Results and Discussion” Section:
Include: The time-domain plot of music.
Include: A table of note frequencies extracted from music, music harmonics, and music amplitude.
Answer: What are the diﬀerences between music, music harmonics, and music amplitude? How do these diﬀerences change the audible sound? How do these diﬀerence relate to instrumental music (roughly)?
Answer: Are the measured note frequencies from music, music harmonics, and music amplitude diﬀerent? If so, how much? Why are they diﬀerent or not diﬀerent?

Part 3: Music Synthesis
Electronic instruments utilize many principles from signals and systems to generate a variety of instruments and dynamics. Electronic keyboards, for example, can often synthesize hundreds of diﬀerent instruments with varying intensities, dynamics, and speeds. These instrumental properties are usually added dynamically through on-board signal processing chips.
Analyze Beethoven: Load the “.mat” ﬁle “beethoven.mat” (using “load beethoven.mat”), which can be downloaded on the course website. The ﬁle will unpack the vector beethoven, which contains a short segment synthetic music sampled at 8000 samples/second.
Listen to beethoven (using sound(beethoven,8000)) and plot (using “plot(beethoven)”) in the time domain. Estimate the locations and durations of each note. Use your function f analysis to record the frequencies of each note in the music.
Synthesize Beethoven: Develop a script or function for generating and playing the beethoven music using only your recorded frequencies, locations, and durations (i.e., synthesize the music). Recreate the signal with diﬀerent harmonics and/or amplitudes. You should use the functions yankee harmonics and yankee amplitude as a guide for accomplishing this. Feel free to experiment (but do not just use pure sinusoids). Save your custom, synthesized music to a “.wav” ﬁle (using wavwrite(custom beethoven, 8000, ‘myname beethoven.wav’)).
Analyze Bach: Load the “.wav” ﬁle “bach.wav” (using “[bach,fs] = wavread(’Bach.wav’)”), which can be downloaded on the course website. The ﬁle will unpack the vector bach, a little over 9 seconds of a piano recording of a J. S. Bach composition. Note that the sampling frequency fs is diﬀerent from what was previously used.
Repeat the analysis steps used for beethoven (extract all of the notes and determine the frequency, location, and duration of each note). Note that this is a much more complicated task and you are not expected to get it perfect. Try to do as best you can. If the analysis takes too long because of the length of the piece, you can pick a shorter portion of the music to analyze. As mentioned in Part 2, it may be easiest to automate some of the tasks for identifying the notes in MATLAB.
Synthesize Bach: Repeat the synthesis steps previously used for beethoven on the new bach frequencies. Save your custom, synthesized music to a “.wav” ﬁle (using wavwrite(custom bach, fs, ‘myname bach.wav’)).
Include in Lab Report “Results and Discussion” Section:
Answer: From the synthesized beethoven music, how does adding harmonics change the music? How might this be useful (hint: look up the word “timbre”)?
Answer: Why is analyzing the bach music more challenging? Discuss the diﬀerences you observed with beethoven. How do these diﬀerences translate into the synthesized music?
Submit: Your custom “.wav” ﬁle for beethoven (should sound diﬀerent than the original)
Submit: Your custom “.wav” ﬁle for bach (should sound diﬀerent than the original)