Two examples of frequency manipulation

A simple synthesizer



A few words about harmony and music



In western music, an octave is the interval between a periodic signal and the same signal with double frequency. A tempered chromatic scale is a geometric progression of frequencies witf ratio 2^(1/12) across one octave. Each step of this progression is called a half tone. The scale is called tempered because there are smaller intervals of sound that enter into consideration if you have a very well trained musical ear. However, taking these small intervals would require two different keys for A# and Bb on a piano, which is not practical. This why a piano is called tempered.
What is of utmost importance is to understand that musical notes (that are derived from a selection of half tones) correspond to signal frequencies that are in geometric progression. To go to the next sharper (higher) halftone, multiply the frequency by 2 at the power of 1/12.

The provided

function [octave,tone]=MusicNote(freq)

returns the nearest half tone (among twelve) and its octave corresponding to the frequency freq, considering that the reference frequency of 440 Hz corresponds to the A note of the third octave of a piano.
The function

function note = number2note(n)

attempts to return the musical notation that corresponds to the tone variable that is produced by the previous function. For instance, the dominant frequency in our sample is at 898.5824 Hz.

Using the routines we obtain
[octave,tone]=MusicNote(898.5824)

octave =
4
tone =
0

and the note is

number2note(0)
ans =
'A'

The scale is called chromatic because all half tones are present in it. Common scales are determined by the intervals measured in tones (i.e. 2 half tones) between each note of the scale.
The western major scale is obtained by using the successive interval, starting from the fundamental
1,1,1/2,1,1,1 for a total 11 half tones, which means that the last note is one half tone from the octave (this why the two are rarely used together)
The melodic minor scale shifts the third and the seventh note according to the following sequence
1,1/2,1,1,1,1/2
and the harmonic minor scale is identical except for the seventh
1,1/2,1,1,1,1
For those who are interested, there is a so called blues scale with the intervals
3/2,1,1/2,1/2,3/2,1/2. The third note is three tones from the fundamental and is the famous blue note.

Before making some music (?), we need to program the following function, which converts a vector of frequencies and amplitudes into a superposition of sine waves.

function x = synth(tone,freqs,amps,numf,nums,Fs,r)
% synthethizes the sound given by the frequencies freqs and their
% amplitude amps, with a shift in a number tone of musical half tones
% numf is the number of frequencies
% nums is the number of samples; Fs the sampling frequency
% normalized by the largest amplitude
% amplitudes are in decreasing order
% r is an amplification (or attenuation) factor
% that is applied to the normalized signal

You will need the halftones routine, that shifts the frequency according to the desired number of half tones.