How do i create a comparison loop of two signals based on their spectral centroid value, with a sliding filter dependant on outcome to balance the amended spectrum

3 views (last 30 days)
this is currently the code i have where the loop doesnt really do what i'd like, but im not sure how is best to loop it correctly. what i need is to compare spectral centroid (SC) A against spectral centroid B which has been run through a filter the reduce the fundamental frequencies. where SC B is now above SC A then a shelf filter should be run on spectrum B reducing the higher frequencies by 1db to reduce the SC down to the same (or withing a small margin of error) as SC A. ideally the shelf filter would start at the highest frequency and then slide downwards, until the SC are the same. would anybody know a reasonable way of doing this?
%% read audio file
[pop,fs] = audioread("Pop Base.wav");
%% SC of Base sound
centroidA = spectralCentroid(pop,fs);
ScA1 = mean(centroidA);
ScA = ScA1(:,1)
%% Fundamental Reduction
gain1 = -6;
slope1 = 2;
Fc1 = 50;
lowFilt = shelvingFilter(gain1,slope1,Fc1,"lowpass");
%visualize(lowFilt)
popHP = lowFilt(pop);
%outcome called popHP
%% SC of ammended Sound
centroidB = spectralCentroid(popHP,fs);
ScB1 = mean(centroidB);
ScB = ScB1(:,1)
%% loop for spectral sweeping
if ScB > ScA
% apply LP shelf filter
gain2 = -1;
slope2 = 2;
Fc2 = ScA;
highFilt = shelvingFilter(gain2,slope2,Fc2,"highpass");
%visualize(highFilt)
popLP = highFilt(popHP);
centroidB = spectralCentroid(popLP,fs);
ScB1 = mean(centroidB);
ScB = ScB1(:,1)
end
audiowrite("C:\Users\peter.wheeler\OneDrive - Acoustic Consultants\" + ...
"Documents\MATLAB\poptest.wav",popLP,fs)

Accepted Answer

Brian Hemmat
Brian Hemmat on 28 Jun 2023
Hi Peter, I'm not sure I fully understand your problem statement. Here's some sample code for what I think you're asking about, but please let me know if I'm misinterpretting.
% Read audio file and convert to mono
[x,fs] = audioread("WashingMachine-16-44p1-stereo-10secs.wav");
x = mean(x,2);
% Calculate mean spectral centroid
% This is the target spectral centroid
SC_A = spectralCentroid(x,fs);
SC_A = mean(SC_A,"all",'omitmissing');
% Filter signal
gain1 = -6;
slope1 = 2;
Fc1 = 50;
lowFilt = shelvingFilter(gain1,slope1,Fc1,"lowpass",SampleRate=fs);
visualize(lowFilt)
popHP = lowFilt(x);
% Calculate mean spectral centroid of filtered signal.
% You want to apply a high-pass filter so that the spectral centroid of
% the filtered signal is similar to the spectral centroid of the original
% signal (SC_A)
SC_B = spectralCentroid(popHP,fs);
SC_B = mean(SC_B,"all","omitmissing");
% Design initial filter. Start at the highest frequency
gain2 = -1;
slope2 = 2;
Fc2 = fs/2;
highFilt = shelvingFilter(gain2,slope2,Fc2,"highpass",SampleRate=fs);
visualize(highFilt)
% Define parameters for the sweep.
threshold = 1;
frequencystep = 400;
frequencyStepReduction = 2;
% Run the sweep until threshold met
while abs(SC_B - SC_A) > threshold
% Apply
popLP = highFilt(popHP);
SC_B = spectralCentroid(popLP,fs);
SC_B = mean(SC_B,"all","omitmissing");
if SC_B < SC_A
% Overshot, reduce frequency step
SC_B = inf;
highFilt.CutoffFrequency = highFilt.CutoffFrequency + frequencystep; % return to before overshoot
frequencystep = frequencystep/frequencyStepReduction;
else
highFilt.CutoffFrequency = highFilt.CutoffFrequency - frequencystep;
end
end
% Look at the final parameters of the high-shelf filter.
highFilt.CutoffFrequency = highFilt.CutoffFrequency - frequencystep
% Compare spectral centroids of the target, filtered, and re-filtered signals
SC_A = mean(spectralCentroid(x,fs),"all","omitmissing")
SC_B = mean(spectralCentroid(popHP,fs),"all","omitmissing")
SC_C = mean(spectralCentroid(popLP,fs),"all","omitmissing")
% Listen to the results.
soundsc(x,fs),pause(size(x,1)/fs+0.5)
soundsc(popHP,fs),pause(size(x,1)/fs+0.5)
soundsc(popLP,fs),pause(size(x,1)/fs+0.5)
  1 Comment
Peter Wheeler
Peter Wheeler on 1 Jul 2023
Hi Brian,
thank you for this. for the most part i think this covers everything. i'll just do a little bit of tweaking for final outcome. i appreciate the help

Sign in to comment.

More Answers (0)

Categories

Find more on Measurements and Spatial Audio in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!