%% Frequencies of Musical Chord
% Define and label the frequencies of a range of musical tones.

C3 = 440 / (2^(21/12));
nOctaves = 3;
musicalTones = C3 * 2.^((0:(12*nOctaves-1))/12);
toneNames = ["C";"C#";"D";"D#";"E";"F";"F#";"G";"G#";"A";"A#";"B"] + string(3:(3+nOctaves-1));
toneNames = categorical(toneNames, toneNames);

% Define the audio signal sampling frequency in Hz, the sample points n, and a signal containing a major chord X.

fs = 16e3;
n = 1:16000;
X = 4*cos(2*pi*(440/fs)*n) + 2*cos(2*pi*(554.37/fs)*n) + 3*cos(2*pi*(659.2/fs)*n);

% Compute and plot the frequency components of the major chord.

Y = nufft(X,[],musicalTones/fs);
bar(toneNames(:),abs(Y))

% Copyright 2019 The MathWorks, Inc.