How to plot Chroma_Features for an audio file?
%% Read in the file
clearvars;
close all;
file_path = ‘C:/Users/nimae/Downloads/audio_2024-05-12_17-59-56.ogg’;
[audio_data, sample_rate] = audioread(file_path);
%% Play original file
% pOrig = audioplayer(audio_data, sample_rate);
% pOrig.play;
%% Plot both audio channels
N = size(audio_data,1); % Determine total number of samples in audio file
figure;
subplot(2,1,1);
stem(1:N, audio_data(:,1));
title(‘Left Channel’);
subplot(2,1,2);
% Increase resolution by increasing the number of points in FFT
N = 2^nextpow2(length(audio_data(:,1)));
% Plot the spectrum with increased resolution
df = sample_rate / N;
w = (-N/2 : N/2 – 1) * df;
y = fft(audio_data(:,1), N) / N; % For normalizing, but not needed for our analysis
y2 = fftshift(y);
% Define the desired frequency range
f_min = 0; % Minimum frequency in Hz
f_max = 5.6e4; % Maximum frequency in Hz
% Find the corresponding indices in the frequency axis
idx_min = find(w >= f_min, 1, ‘first’);
idx_max = find(w <= f_max, 1, ‘last’);
% Plot the spectrum with increased resolution and within the desired frequency range
figure;
plot(w(idx_min:idx_max), abs(y2(idx_min:idx_max)));
xlabel(‘Frequency (Hz)’);
ylabel(‘Magnitude’);
title(‘Spectrum of Audio Signal’);
grid on;
hold on; % Add subsequent plots to the same figure
% Frequencies you provided
frequencies = [
16.35 32.702 65.404 130.808 261.616 523.232 1046.464 2092.928 4185.856 8371.712;
17.32 34.648 69.296 138.592 277.184 554.368 1108.736 2217.472 4434.944 8869.888;
18.35 36.708 73.416 146.832 293.664 587.328 1174.656 2349.312 4698.624 9397.248;
19.45 38.89 77.78 155.56 311.12 622.24 1244.48 2488.96 4977.92 9955.84;
20.60 41.202 82.404 164.808 329.616 659.232 1318.464 2636.928 5273.856 10547.712;
21.83 43.654 87.308 174.616 349.232 698.464 1396.928 2793.856 5587.712 11175.424;
23.12 46.248 92.496 184.992 369.984 739.968 1479.936 2959.872 5919.744 11839.488;
24.50 48.998 97.996 195.992 391.984 783.968 1567.936 3135.872 6271.744 12543.488;
25.96 51.912 103.824 207.648 415.296 830.592 1661.184 3322.368 6644.736 13289.472;
27.50 55 110 220 440 880 1760 3520 7040 14080;
29.14 58.27 116.54 233.08 466.16 932.32 1864.64 3729.28 7458.56 14917.12;
30.87 61.736 123.472 246.944 493.888 987.776 1975.552 3951.104 7902.208 15804.416
];
% Plot the frequencies you provided
for i = 1:size(frequencies, 1)
plot(frequencies(i, :), zeros(size(frequencies, 2)), ‘o’, ‘MarkerSize’, 5);
end
legend(‘Spectrum’, ‘Frequencies provided’);
% Convert frequencies to chroma values
chroma_values = mod(round(log2(frequencies/440) * 12), 12);
% Plot the chroma diagram
figure;
imagesc(chroma_values);
colormap(jet);
colorbar;
xlabel(‘Time’);
ylabel(‘Chroma’);
title(‘Chroma Diagram’);
function Y = chromagram_IF(d,sample_rate,fftlen,~,f_ctr,f_sd)
%% Function Definitions
% Calculate the chroma matrix. Use a long FFT to discriminate
% spectral lines as well as possible (2048 is the default value)
cfftlen=2048;
C = chromagram_IF(audio_data,sample_rate,cfftlen);
% The frame advance is always one quarter of the FFT length. Thus,
% the columns of C are at timebase of fftlen/4/sr
tt = (1:size(C,2))*cfftlen/4/sample_rate;
% Plot spectrogram using a shorter window
subplot(311)
sfftlen = 512;
specgram(audio_data,sfftlen,sample_rate);
% Always use a 60 dB colormap range
clim(max(clim)+[-60 0])
% .. and look only at the bottom 4 kHz of spectrum
axis([0 length(d)/sample_rate 0 4000])
title(‘Original Sound’)
% Now the chromagram, also on a dB magnitude scale
subplot(312)
imagesc(tt,1:12,20*log10(C+eps));
axis xy
clim(max(clim)+[-60 0])
title(‘Chromagram’)
% Y = chromagram_IF(d,sr,fftlen,nbin,f_ctr,f_sd)
% Calculate a "chromagram" of the sound in d (at sampling rate sr)
% Use windows of fftlen points, hopped by ffthop points
% Divide the octave into nbin steps
% Weight with center frequency f_ctr (in Hz) and gaussian SD f_sd
% (in octaves)
% Use instantaneous frequency to keep only real harmonics.
% 2006-09-26 dpwe@ee.columbia.edu
% Copyright (c) 2006 Columbia University.
%
% This file is part of LabROSA-coversongID
%
% LabROSA-coversongID is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 2 as
% published by the Free Software Foundation.
%
% LabROSA-coversongID is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
% General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with LabROSA-coversongID; if not, write to the Free Software
% Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
% 02110-1301 USA
%
% See the file "COPYING" for the text of the license.
if nargin < 3; fftlen = 2048; end
%if nargin < 4; nbin = 12; end
if nargin < 5; f_ctr = 1000; end
if nargin < 6; f_sd = 1; end
%A0 = 27.5; % Hz
%A440 = 440; % Hz
%f_ctr_log = log(f_ctr/A0) / log(2);
fminl = octs2hz(hz2octs(f_ctr)-2*f_sd);
fminu = octs2hz(hz2octs(f_ctr)-f_sd);
fmaxl = octs2hz(hz2octs(f_ctr)+f_sd);
fmaxu = octs2hz(hz2octs(f_ctr)+2*f_sd);
%ffthop = fftlen/4;
nchr = 12;
% Calculate spectrogram and IF gram pitch tracks…
[p,m]=ifptrack(d,fftlen,sr,fminl,fminu,fmaxl,fmaxu);
[~,ncols] = size(p);
%disp([‘ncols = ‘,num2str(ncols)]);
% chroma-quantized IF sinusoids
Pocts = hz2octs(p+(p==0));
Pocts(p(:)==0) = 0;
% Figure best tuning alignment
nzp = find(p(:)>0);
%hist(nchr*Pmapo(nzp)-round(nchr*Pmapo(nzp)),100)
[hn,hx] = histogram(nchr*Pocts(nzp)-round(nchr*Pocts(nzp)),100);
centsoff = hx(hn == max(hn));
% Adjust tunings to align better with chroma
Pocts(nzp) = Pocts(nzp) – centsoff(1)/nchr;
% Quantize to chroma bins
PoctsQ = Pocts;
PoctsQ(nzp) = round(nchr*Pocts(nzp))/nchr;
% map IF pitches to chroma bins
Pmapc = round(nchr*(PoctsQ – floor(PoctsQ)));
Pmapc(p(:) == 0) = -1;
Pmapc(Pmapc(:) == nchr) = 0;
Y = zeros(nchr,ncols);
for t = 1:ncols
Y(:,t)=(repmat((0:(nchr-1))’,1,size(Pmapc,1))==repmat(Pmapc(:,t)’,nchr,1))*m(:,t);
end
end
hello,
i have coded this file and copied most of it from Columbian University, as you can see. I want to plot simply the chroma features for my file in variation of time, i mean determining exactly at which time which musical note is played in the file. For that we need at the X axis, time, and in Yaxis Frequencies of those musical notes(which i have provided in the "Frequencies" Matrix).
So I’d be thankful if someone can guide me a little bit through this. My code runs halfway but i don’t get the desired Chroma Diagram, said above.%% Read in the file
clearvars;
close all;
file_path = ‘C:/Users/nimae/Downloads/audio_2024-05-12_17-59-56.ogg’;
[audio_data, sample_rate] = audioread(file_path);
%% Play original file
% pOrig = audioplayer(audio_data, sample_rate);
% pOrig.play;
%% Plot both audio channels
N = size(audio_data,1); % Determine total number of samples in audio file
figure;
subplot(2,1,1);
stem(1:N, audio_data(:,1));
title(‘Left Channel’);
subplot(2,1,2);
% Increase resolution by increasing the number of points in FFT
N = 2^nextpow2(length(audio_data(:,1)));
% Plot the spectrum with increased resolution
df = sample_rate / N;
w = (-N/2 : N/2 – 1) * df;
y = fft(audio_data(:,1), N) / N; % For normalizing, but not needed for our analysis
y2 = fftshift(y);
% Define the desired frequency range
f_min = 0; % Minimum frequency in Hz
f_max = 5.6e4; % Maximum frequency in Hz
% Find the corresponding indices in the frequency axis
idx_min = find(w >= f_min, 1, ‘first’);
idx_max = find(w <= f_max, 1, ‘last’);
% Plot the spectrum with increased resolution and within the desired frequency range
figure;
plot(w(idx_min:idx_max), abs(y2(idx_min:idx_max)));
xlabel(‘Frequency (Hz)’);
ylabel(‘Magnitude’);
title(‘Spectrum of Audio Signal’);
grid on;
hold on; % Add subsequent plots to the same figure
% Frequencies you provided
frequencies = [
16.35 32.702 65.404 130.808 261.616 523.232 1046.464 2092.928 4185.856 8371.712;
17.32 34.648 69.296 138.592 277.184 554.368 1108.736 2217.472 4434.944 8869.888;
18.35 36.708 73.416 146.832 293.664 587.328 1174.656 2349.312 4698.624 9397.248;
19.45 38.89 77.78 155.56 311.12 622.24 1244.48 2488.96 4977.92 9955.84;
20.60 41.202 82.404 164.808 329.616 659.232 1318.464 2636.928 5273.856 10547.712;
21.83 43.654 87.308 174.616 349.232 698.464 1396.928 2793.856 5587.712 11175.424;
23.12 46.248 92.496 184.992 369.984 739.968 1479.936 2959.872 5919.744 11839.488;
24.50 48.998 97.996 195.992 391.984 783.968 1567.936 3135.872 6271.744 12543.488;
25.96 51.912 103.824 207.648 415.296 830.592 1661.184 3322.368 6644.736 13289.472;
27.50 55 110 220 440 880 1760 3520 7040 14080;
29.14 58.27 116.54 233.08 466.16 932.32 1864.64 3729.28 7458.56 14917.12;
30.87 61.736 123.472 246.944 493.888 987.776 1975.552 3951.104 7902.208 15804.416
];
% Plot the frequencies you provided
for i = 1:size(frequencies, 1)
plot(frequencies(i, :), zeros(size(frequencies, 2)), ‘o’, ‘MarkerSize’, 5);
end
legend(‘Spectrum’, ‘Frequencies provided’);
% Convert frequencies to chroma values
chroma_values = mod(round(log2(frequencies/440) * 12), 12);
% Plot the chroma diagram
figure;
imagesc(chroma_values);
colormap(jet);
colorbar;
xlabel(‘Time’);
ylabel(‘Chroma’);
title(‘Chroma Diagram’);
function Y = chromagram_IF(d,sample_rate,fftlen,~,f_ctr,f_sd)
%% Function Definitions
% Calculate the chroma matrix. Use a long FFT to discriminate
% spectral lines as well as possible (2048 is the default value)
cfftlen=2048;
C = chromagram_IF(audio_data,sample_rate,cfftlen);
% The frame advance is always one quarter of the FFT length. Thus,
% the columns of C are at timebase of fftlen/4/sr
tt = (1:size(C,2))*cfftlen/4/sample_rate;
% Plot spectrogram using a shorter window
subplot(311)
sfftlen = 512;
specgram(audio_data,sfftlen,sample_rate);
% Always use a 60 dB colormap range
clim(max(clim)+[-60 0])
% .. and look only at the bottom 4 kHz of spectrum
axis([0 length(d)/sample_rate 0 4000])
title(‘Original Sound’)
% Now the chromagram, also on a dB magnitude scale
subplot(312)
imagesc(tt,1:12,20*log10(C+eps));
axis xy
clim(max(clim)+[-60 0])
title(‘Chromagram’)
% Y = chromagram_IF(d,sr,fftlen,nbin,f_ctr,f_sd)
% Calculate a "chromagram" of the sound in d (at sampling rate sr)
% Use windows of fftlen points, hopped by ffthop points
% Divide the octave into nbin steps
% Weight with center frequency f_ctr (in Hz) and gaussian SD f_sd
% (in octaves)
% Use instantaneous frequency to keep only real harmonics.
% 2006-09-26 dpwe@ee.columbia.edu
% Copyright (c) 2006 Columbia University.
%
% This file is part of LabROSA-coversongID
%
% LabROSA-coversongID is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 2 as
% published by the Free Software Foundation.
%
% LabROSA-coversongID is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
% General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with LabROSA-coversongID; if not, write to the Free Software
% Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
% 02110-1301 USA
%
% See the file "COPYING" for the text of the license.
if nargin < 3; fftlen = 2048; end
%if nargin < 4; nbin = 12; end
if nargin < 5; f_ctr = 1000; end
if nargin < 6; f_sd = 1; end
%A0 = 27.5; % Hz
%A440 = 440; % Hz
%f_ctr_log = log(f_ctr/A0) / log(2);
fminl = octs2hz(hz2octs(f_ctr)-2*f_sd);
fminu = octs2hz(hz2octs(f_ctr)-f_sd);
fmaxl = octs2hz(hz2octs(f_ctr)+f_sd);
fmaxu = octs2hz(hz2octs(f_ctr)+2*f_sd);
%ffthop = fftlen/4;
nchr = 12;
% Calculate spectrogram and IF gram pitch tracks…
[p,m]=ifptrack(d,fftlen,sr,fminl,fminu,fmaxl,fmaxu);
[~,ncols] = size(p);
%disp([‘ncols = ‘,num2str(ncols)]);
% chroma-quantized IF sinusoids
Pocts = hz2octs(p+(p==0));
Pocts(p(:)==0) = 0;
% Figure best tuning alignment
nzp = find(p(:)>0);
%hist(nchr*Pmapo(nzp)-round(nchr*Pmapo(nzp)),100)
[hn,hx] = histogram(nchr*Pocts(nzp)-round(nchr*Pocts(nzp)),100);
centsoff = hx(hn == max(hn));
% Adjust tunings to align better with chroma
Pocts(nzp) = Pocts(nzp) – centsoff(1)/nchr;
% Quantize to chroma bins
PoctsQ = Pocts;
PoctsQ(nzp) = round(nchr*Pocts(nzp))/nchr;
% map IF pitches to chroma bins
Pmapc = round(nchr*(PoctsQ – floor(PoctsQ)));
Pmapc(p(:) == 0) = -1;
Pmapc(Pmapc(:) == nchr) = 0;
Y = zeros(nchr,ncols);
for t = 1:ncols
Y(:,t)=(repmat((0:(nchr-1))’,1,size(Pmapc,1))==repmat(Pmapc(:,t)’,nchr,1))*m(:,t);
end
end
hello,
i have coded this file and copied most of it from Columbian University, as you can see. I want to plot simply the chroma features for my file in variation of time, i mean determining exactly at which time which musical note is played in the file. For that we need at the X axis, time, and in Yaxis Frequencies of those musical notes(which i have provided in the "Frequencies" Matrix).
So I’d be thankful if someone can guide me a little bit through this. My code runs halfway but i don’t get the desired Chroma Diagram, said above. %% Read in the file
clearvars;
close all;
file_path = ‘C:/Users/nimae/Downloads/audio_2024-05-12_17-59-56.ogg’;
[audio_data, sample_rate] = audioread(file_path);
%% Play original file
% pOrig = audioplayer(audio_data, sample_rate);
% pOrig.play;
%% Plot both audio channels
N = size(audio_data,1); % Determine total number of samples in audio file
figure;
subplot(2,1,1);
stem(1:N, audio_data(:,1));
title(‘Left Channel’);
subplot(2,1,2);
% Increase resolution by increasing the number of points in FFT
N = 2^nextpow2(length(audio_data(:,1)));
% Plot the spectrum with increased resolution
df = sample_rate / N;
w = (-N/2 : N/2 – 1) * df;
y = fft(audio_data(:,1), N) / N; % For normalizing, but not needed for our analysis
y2 = fftshift(y);
% Define the desired frequency range
f_min = 0; % Minimum frequency in Hz
f_max = 5.6e4; % Maximum frequency in Hz
% Find the corresponding indices in the frequency axis
idx_min = find(w >= f_min, 1, ‘first’);
idx_max = find(w <= f_max, 1, ‘last’);
% Plot the spectrum with increased resolution and within the desired frequency range
figure;
plot(w(idx_min:idx_max), abs(y2(idx_min:idx_max)));
xlabel(‘Frequency (Hz)’);
ylabel(‘Magnitude’);
title(‘Spectrum of Audio Signal’);
grid on;
hold on; % Add subsequent plots to the same figure
% Frequencies you provided
frequencies = [
16.35 32.702 65.404 130.808 261.616 523.232 1046.464 2092.928 4185.856 8371.712;
17.32 34.648 69.296 138.592 277.184 554.368 1108.736 2217.472 4434.944 8869.888;
18.35 36.708 73.416 146.832 293.664 587.328 1174.656 2349.312 4698.624 9397.248;
19.45 38.89 77.78 155.56 311.12 622.24 1244.48 2488.96 4977.92 9955.84;
20.60 41.202 82.404 164.808 329.616 659.232 1318.464 2636.928 5273.856 10547.712;
21.83 43.654 87.308 174.616 349.232 698.464 1396.928 2793.856 5587.712 11175.424;
23.12 46.248 92.496 184.992 369.984 739.968 1479.936 2959.872 5919.744 11839.488;
24.50 48.998 97.996 195.992 391.984 783.968 1567.936 3135.872 6271.744 12543.488;
25.96 51.912 103.824 207.648 415.296 830.592 1661.184 3322.368 6644.736 13289.472;
27.50 55 110 220 440 880 1760 3520 7040 14080;
29.14 58.27 116.54 233.08 466.16 932.32 1864.64 3729.28 7458.56 14917.12;
30.87 61.736 123.472 246.944 493.888 987.776 1975.552 3951.104 7902.208 15804.416
];
% Plot the frequencies you provided
for i = 1:size(frequencies, 1)
plot(frequencies(i, :), zeros(size(frequencies, 2)), ‘o’, ‘MarkerSize’, 5);
end
legend(‘Spectrum’, ‘Frequencies provided’);
% Convert frequencies to chroma values
chroma_values = mod(round(log2(frequencies/440) * 12), 12);
% Plot the chroma diagram
figure;
imagesc(chroma_values);
colormap(jet);
colorbar;
xlabel(‘Time’);
ylabel(‘Chroma’);
title(‘Chroma Diagram’);
function Y = chromagram_IF(d,sample_rate,fftlen,~,f_ctr,f_sd)
%% Function Definitions
% Calculate the chroma matrix. Use a long FFT to discriminate
% spectral lines as well as possible (2048 is the default value)
cfftlen=2048;
C = chromagram_IF(audio_data,sample_rate,cfftlen);
% The frame advance is always one quarter of the FFT length. Thus,
% the columns of C are at timebase of fftlen/4/sr
tt = (1:size(C,2))*cfftlen/4/sample_rate;
% Plot spectrogram using a shorter window
subplot(311)
sfftlen = 512;
specgram(audio_data,sfftlen,sample_rate);
% Always use a 60 dB colormap range
clim(max(clim)+[-60 0])
% .. and look only at the bottom 4 kHz of spectrum
axis([0 length(d)/sample_rate 0 4000])
title(‘Original Sound’)
% Now the chromagram, also on a dB magnitude scale
subplot(312)
imagesc(tt,1:12,20*log10(C+eps));
axis xy
clim(max(clim)+[-60 0])
title(‘Chromagram’)
% Y = chromagram_IF(d,sr,fftlen,nbin,f_ctr,f_sd)
% Calculate a "chromagram" of the sound in d (at sampling rate sr)
% Use windows of fftlen points, hopped by ffthop points
% Divide the octave into nbin steps
% Weight with center frequency f_ctr (in Hz) and gaussian SD f_sd
% (in octaves)
% Use instantaneous frequency to keep only real harmonics.
% 2006-09-26 dpwe@ee.columbia.edu
% Copyright (c) 2006 Columbia University.
%
% This file is part of LabROSA-coversongID
%
% LabROSA-coversongID is free software; you can redistribute it and/or modify
% it under the terms of the GNU General Public License version 2 as
% published by the Free Software Foundation.
%
% LabROSA-coversongID is distributed in the hope that it will be useful, but
% WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
% General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with LabROSA-coversongID; if not, write to the Free Software
% Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
% 02110-1301 USA
%
% See the file "COPYING" for the text of the license.
if nargin < 3; fftlen = 2048; end
%if nargin < 4; nbin = 12; end
if nargin < 5; f_ctr = 1000; end
if nargin < 6; f_sd = 1; end
%A0 = 27.5; % Hz
%A440 = 440; % Hz
%f_ctr_log = log(f_ctr/A0) / log(2);
fminl = octs2hz(hz2octs(f_ctr)-2*f_sd);
fminu = octs2hz(hz2octs(f_ctr)-f_sd);
fmaxl = octs2hz(hz2octs(f_ctr)+f_sd);
fmaxu = octs2hz(hz2octs(f_ctr)+2*f_sd);
%ffthop = fftlen/4;
nchr = 12;
% Calculate spectrogram and IF gram pitch tracks…
[p,m]=ifptrack(d,fftlen,sr,fminl,fminu,fmaxl,fmaxu);
[~,ncols] = size(p);
%disp([‘ncols = ‘,num2str(ncols)]);
% chroma-quantized IF sinusoids
Pocts = hz2octs(p+(p==0));
Pocts(p(:)==0) = 0;
% Figure best tuning alignment
nzp = find(p(:)>0);
%hist(nchr*Pmapo(nzp)-round(nchr*Pmapo(nzp)),100)
[hn,hx] = histogram(nchr*Pocts(nzp)-round(nchr*Pocts(nzp)),100);
centsoff = hx(hn == max(hn));
% Adjust tunings to align better with chroma
Pocts(nzp) = Pocts(nzp) – centsoff(1)/nchr;
% Quantize to chroma bins
PoctsQ = Pocts;
PoctsQ(nzp) = round(nchr*Pocts(nzp))/nchr;
% map IF pitches to chroma bins
Pmapc = round(nchr*(PoctsQ – floor(PoctsQ)));
Pmapc(p(:) == 0) = -1;
Pmapc(Pmapc(:) == nchr) = 0;
Y = zeros(nchr,ncols);
for t = 1:ncols
Y(:,t)=(repmat((0:(nchr-1))’,1,size(Pmapc,1))==repmat(Pmapc(:,t)’,nchr,1))*m(:,t);
end
end
hello,
i have coded this file and copied most of it from Columbian University, as you can see. I want to plot simply the chroma features for my file in variation of time, i mean determining exactly at which time which musical note is played in the file. For that we need at the X axis, time, and in Yaxis Frequencies of those musical notes(which i have provided in the "Frequencies" Matrix).
So I’d be thankful if someone can guide me a little bit through this. My code runs halfway but i don’t get the desired Chroma Diagram, said above. chromatography, audio MATLAB Answers — New Questions