Librosa MFCC feature extraction - mfcc

I wanna extract the mfcc futures with librosa library. anyone can explain to me what is the difference between librosa.feature.mfcc(y=audio, sr=sr, n_mfcc=40) and np.mean(librosa.feature.mfcc(y=audio, sr=sr, n_mfcc=40).T,axis=0) ???

I think its each coefficient averaged over time.
So you will get an Output of 40 values

np.mean() or numpy.mean() will returns the average of the array elements. So if you use np.mean() to your mfcc extraction, it will be return the average of it.
In np.mean(), axis = 0 means along the column and axis = 1 means working along the row. For example:
a = np.array([[1, 2], [3, 4]])
np.mean(a) // 2.5
np.mean(a, axis=0) // array([2., 3.])
np.mean(a, axis=1) // array([1.5, 3.5])
Reference:
numpy.mean() docs
more about numpy.mean() - geeksforgeeks.org

Related

Is there a way to use scipy.interpolate to do 1d spline interpolation with boundary conditions specified away from data?

There are lots of ways of assembling splines directly from data while specifying derivatives at the boundary but I do not see an easy way to specify derivative constraints away from the data. For example
np.random.seed(0)
M = 30
x = np.random.rand(M)
x.sort()
y = np.random.rand(M) + (10 * x) ** 2
xx = np.linspace(x.min(), x.max(), 100) # for plotting
import scipy.interpolate as si
f = si.make_interp_spline(x, y) # ok but how to add derivative conditions away from the x-values?
x_derivs = [-1, 2]
order_derivs = [2, 1]
value_derivs = [0, 1]
More generally, is there a built in method to do this or take derivative constraints separate from value constraints?
I think it's just a matter of creating the operator matrix to solve the linear problem.
Short answer : it's not implemented at the moment.
You can of course modify the scipy source, the relevant part is
https://github.com/scipy/scipy/blob/v1.7.0/scipy/interpolate/_bsplines.py#L1105

Can anyone tell me how to write indicator function for vector input in matlab?

I am a new user of matlab and stackoverflow. I asked a question about how to write indicator function f, the question was as follow:
"f should be an anonymous function like f=#(t)1[0,0.25)(t). However, the number of intervals for the piecewise constant function is not fixed in general. Instead, the piecewise interval depends on users input."
I am glad that some users answered my question.
The solution answered is as follow:
%defines weight vector. for example: a1=1, a2=2, a3=3, a4=4,a5=5
A = 1:5;
%defines a range vector
ranges = [0:(1/length(A)):1,inf];
%The padding is for handling cases where t<0 or t>=1
APadded = [0,A,0];
f=#*(t)APadded(find(t < ranges,1,'first'));
Result
f(0.1) = 1, f(0.3) = 2, f(0.5) = 3, f(0.7) = 4, f(0.9) = 5, f(-0.1) = f(1.1) = 0;
I want to extend the codes a little bit. For example, if I define #(X)sun(X), when the input of X is a vector, it gives output to be a vector. However, the above solution for indicator result does not. One can try f([0.1 0.3 0.5 0.7 0.9]) which does not give 1, 2, 3, 4, 5.
How to solve this problem so that even if the input is a vector, it will give correct result?
Use Matlab's arrayfun function on the created f function:
f2=#(t)arrayfun(f,t);
Result:
f2([-0.1, 0.1,0.3,0.5,0.7,0.9,1.1])
ans =
0 1 2 3 4 5 0
Full code example:
%defines weight vector. for example: a1=1, a2=2, a3=3, a4=4,a5=5
A = 1:5;
%defines a range vector
ranges = [0:(1/length(A)):1,inf];
%The padding is for handling cases where t<0 or t>=1
APadded = [0,A,0];
fTemp=#(t)APadded(find(t < ranges,1,'first'));
f=#(t)arrayfun(fTemp,t);

How to get cumulative distribution functions of a vector in Matlab using cumsum?

I want to get the probability to get a value X higher than x_i, which means the cumulative distribution functions CDF. P(X>=x_i).
I've tried to do it in Matlab with this code.
Let's assume the data is in the column vector p1.
xp1 = linspace(min(p1), max(p1)); %range of bins
histp1 = histc(p1(:), xp1); %histogram od data
probp1 = histp1/sum(histp1); %PDF (probability distribution function)
`figure;plot(probp1, 'o') `
Now I want to calculate the CDF,
sorncount = flipud(histp1);
cumsump1 = cumsum(sorncount);
normcumsump1 = cumsump1/max(cumsump1);
cdf = flipud(normcumsump1);
figure;plot(xp1, cdf, 'ok');
I'm wondering whether anyone can help me to know if I'm ok or am I doing something wrong?
Your code works correctly, but is a bit more complicated than it could be. Since probp1 has been normalized to have sum equal to 1, the maximum of its cumulative sum is guaranteed to be 1, so there is no need to divide by this maximum. This shortens the code a bit:
xp1 = linspace(min(p1), max(p1)); %range of bins
histp1 = histc(p1(:), xp1); %count for each bin
probp1 = histp1/sum(histp1); %PDF (probability distribution function)
cdf = flipud(cumsum(flipud(histp1))); %CDF (unconventional, of P(X>=a) kind)
As Raab70 noted, most of the time CDF is understood as P(X<=a), in which case you don't need flipud: taking cumsum(histp1) is all that's needed.
Also, I would probably use histp1(end:-1:1) instead of flipud(histp1), so that the vector is flipped no matter if it's a row or column.

How to use classifiers

I want to use the svm, knn, adaboost classifier on my data features. I build up code where I calculated the frame differences and calculated the features (eigenvalues, strain energy, potential energy).... build up an array of [number of frames , features]. I try to use svm as:
Features = data; % Features array [40, 5]
class = ones(numFrames-1, 1); % numFrames=41
class(1:(fix(numFrames/2))) = -1;
SVMstruct = svmtrain(Features, class, 'Kernel_Function', 'rbf');
newclass = svmclassify(SVMstruct, [40 5]); %Test data
I got an error:
The number of columns in TEST and training data must be equal.
%classperf(cp,newclass); %performance of the class given by cp'`
What is the reason for this error? And how do I to use further classifiers with this features set?
I can infer following things from the error which you are getting.
There is no error in svmtrain that means size(features)=[40 5]. The error is in the last line. See the syntax of svmclassify. You pass a sample of test data which has same number of features/columns as the training data in your case 5). Instead you are passing the size which is [40 5] which has only two columns. Pass the actual test set of n rows and 5 columns. The last line should be
newclass= svmclassify(SVMstruct,testData); %where size(testData)=[n 5], n indicates how many test samples you have.

why is the vector coming out of 'trapz' function as NAN?

i am trying to calculate the inverse fourier transform of the vector XRECW. for some reason i get a vector of NANs.
please help!!
t = -2:1/100:2;
x = ((2/5)*sin(5*pi*t))./((1/25)-t.^2);
w = -20*pi:0.01*pi:20*pi;
Hw = (exp(j*pi.*(w./(10*pi)))./(sinc(w./(10*pi)))).*(heaviside(w+5*pi)-heaviside(w-5*pi));%low pass filter
xzohw = 0;
for q=1:20:400
xzohw = xzohw + x(q).*(2./w).*sin(0.1.*w).*exp(-j.*w*0.2*((q-1)/20)+0.5);%calculating fourier transform of xzoh
end
xzohw = abs(xzohw);
xrecw = abs(xzohw.*Hw);%filtering the fourier transform high frequencies
xrect=0;
for q=1:401
xrect(q) = (1/(2*pi)).*trapz(xrecw.*exp(j*w*t(q))); %inverse fourier transform
end
xrect = abs(xrect);
plot(t,xrect)
Here's a direct answer to your question of "why" there is a nan. If you run your code, the Nan comes from dividing by zero in line 7 for computing xzohw. Notice that w contains zero:
>> find(w==0)
ans =
2001
and you can see in line 7 that you divide by the elements of w with the (2./w) factor.
A quick fix (although it is not a guarantee that your code will do what you want) is to avoid including 0 in w by using a step which avoids zero. Since pi is certainly not divisible by 100, you can try taking steps in .01 increments:
w = -20*pi:0.01:20*pi;
Using this, your code produces a plot which might resemble what you're looking for. In order to do better, we might need more details on exactly what you're trying to do, or what these variables represent.
Hope this helps!