Find Audio Peaks in MATLAB [closed] - matlab

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have an audio signal of about the size 7000000 x 1. I have used the peakfinder m file in MATLAB to find the location of all of the peaks in the audio file above a specific threshold. I am now trying to find a frame sized 1000000 x 1 that contains the greatest amount of peaks. I am completely lost on how to do this and any help would be greatly appreciated. Thank you!

Well, all the peak finder function is doing is taking the second derivative and looking for any place where the resulting value is negative. This indicates a local maximum. So you can do something very similar to find any local maximum.
Once you have these indices, you can window the array containing a logical representation of the locations, and count how many peaks are there.
The code below will do what I am saying. It will window across and count the number of peaks found, and return a a vector of the counts, which you can then just find the max of, and then you have the starting index.
clc; close all; clear all;
A = randi(10,[1,100])
plot(A)
hold on
C = diff(diff(A))
indices = find(C < 0)+1;
scatter(indices,A(indices),'r')
temp = zeros(size(A));
temp(indices) = 1;
window = ones(1,5);
results = conv(temp,window,'same');
max(results)
This is of course a pet example, A would be your matrix, and window would be a matrix the length of the range you want to examine, in your case 1000000
Edit
As Try Hard has made note of in the comments below, this method will be fairly susceptible to noise, so what you can do first is run a smoothing filter over the signal before doing any derivatives, something like as follows.
filt = (1/filtLength) * ones(1,filtLength);
A = conv(A,filt,'same')
This is a simple averaging filter which will help smooth out some of the noise

Related

How to recover original data after detrending? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a vector y which contains the original data I have collected. I am using the MATLAB function detrend(..) in order to shift the data around zero and be able to compare them with another vector x which contains data which exist around zero. Now, what I want to do is to shift the data of vector y back to their original scale and also if possible shift the data of vector x as well in order to compare them around the area of the original data of y. The reason for that is because I want to compute the relative error between the data which is given by the formula:
It is obvious that x can't be around zero because it will cause division problems. I have tried to recover the data by adding to the detrending data the mean of the original data but it doesn;t give me back the original data. Is there any way I can do so ?
Detrending removes both offsets and linear trends from the data, so adding back the mean is not sufficient. You also have to add points from a line of the form d = a*t + b.
The two-output version of the detrend command returns the portion of the data, T_r that was removed from the original data:
[data_d,T_r] = detrend(___)
So unless you have access to T_r mentioned above or the equation of the line that was removed from the original data, you cannot recover it back.
You can see in MATLAB's documentation that a trend line is removed with the detrend command: https://www.mathworks.com/help/ident/ref/detrend.html

MATLAB - singularity warning [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
when my matlab code gets to the line:
vE(:,:,i)=(mY(:,:,i))\(-mA*(vIs-mG(:,:,i)*vVs));
The following warning comes up:
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate.
RCOND = 1.682710e-16.
Whats wrong?
Full code:
function [ vE, vV_node, vI_node ] = ...
node_analysis( vIs, vVs, mA, mG, mY )
[A,B,N]=size(mY);
vE=zeros(4,1,N);
for i=1:N
vE(:,:,i)=(mY(:,:,i))\(-mA*(vIs-mG(:,:,i)*vVs));
vV_node(:,:,i)=mA'*vE(:,:,i);
vI_node(:,:,i)=mG(:,:,i)*vV_node(:,:,i)+(vIs-mG(:,:,i)*vVs);
end
end
vE=mY^-1 * (-mA*(cIs-mG*vVs))
vE is (4x1xN) size
mY(4x4xN)
mA(4x9)
vIs(9x1)
mG(9x9xN)
vVs(9x1)
When you use the \ operator with a matrix, MATLAB will try and solve the least squares problem to estimate x given y in the equation y = A*x. Depending on the size and shape of A, solving this equation might be easy, hard, or impossible without additional information. It just depends on your particular problem.
As Oli mentioned the comments, this is because your matrix is close to singular or its singular values are close to zero. MATLAB is properly informing you that the MATRIX likely has either unknown information that is going to screw up the answer or that some of the information in the MATRIX is so small compared to other pieces that the small part is going to make solving for x almost impossible and error prone.
Depending on your math background, you might consider the following cod where I create create a matrix with one value very small. This will reproduce your error:
%% Make some data:
randn('seed', 1982);
n = 3;
A = zeros(n);
for ind = 1:n-1
v = randn(n,1);
A = A + v*v';
end
% Last bit is very tiny compared to the others:
A = A + 1e-14*randn(n,1)*randn(1,n);
%% Try and solve Ax=y for x= 1,2,3...
x = (1:n)';
y = A*x
x_est = A \ y
There are various ways to start trying to fix this, usually by reformulating the problem and/or adding some kind of regularization term. A good first try, though, is so add a simple Tikhonov regularization which bumps up all the small values to something reasonable that MATLAB can work with. This may mess up your data but you can plat with it.
Roughly, try this:
tikk = 1e-12;
x_est2 = (A + tikk * eye(n)) \ y
For larger or smaller values of tikk and you will see the error goes away but the solution is to some degree wrong. You might find this acceptable or not.
Note that in my example the answer is quite wrong because I used n=3. As you increase the problem size n you will be better results.
Finally, to begin exploring what is wrong with your matrix A ((-mA*(vIs-mG(:,:,i)*vVs))), you might consider seeing how fast the values s in s=svd(A) decay. Some of them should be quite close to zero. Also, you might look at Tihkonov regularization and what you can do by actually decomposing the matrix into the SVD and scaling things better.

Matlab cosine mistake [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've got some problem while equaling such function as:
function czeb()
k = 1:1:5;
Xk = cos( (pi*(k-0.5))/5);
CN(5,Xk)
end
function c = CN(N,x)
a=N*acos(x); % a is equal correct
c = cos(a); % buc c not, why?
return
end
If I view variable a inside CN function I receive
a=[1.5708, 4.7124, 7.8540, 10.9956, 14.1372]
which is correct bun next step in CN function is to calculate cos(a).
In this step I receive incorrect value of cos(a).
It should be
cos(a) = 1.0e-04 *[-0.0367,0.1102,-0.1837,0.2571,-0.3306]
but it is 1.0e-15 * [-0.8269,-0.1837,0.3062,-0.4286,0.5511] and I don't know why...
There is a very simple explanation, and you function is not wrong.
a=[1.5708, 4.7124, 7.8540, 10.9956, 14.1372]
is equal to pi/2 + k * pi. When you take the cosine of a, you will just get zeros. 1.0e-15 * 0.8269 is essentially zero (floating point arithmetic, and rounding errors).
I think, I understand your. Have you have create expected values manual, like cos(1.5708)? If yes, then you will always receive different results with results made by your computer. So, I think, your expected values of array a are incorrect.
First, enable long number format in the MATLAB/Octave:
format long;
After that, if you display your a array, you will see the similar output:
> a
a =
1.57079632679490 4.71238898038469 7.85398163397448 10.99557428756428 14.13716694115407
As you see at this step, your expected values and computed values by MATLAB/Octave are different. Than, if you do cos(1.57079632679490) the result will like -3.49148336110938e-15 and, as you see, this is near to the result of cos(a(1)): -8.26948102009006e-16. This means, that your stored number is different with that you see in the output.
To instead receive the same results as you expect - round your a to the fourth number after comma:
a = round(a*10000)/10000;
With this the computed results should be near to your expected results.

Matlab exercise: i just don't get it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Given the signals:
f1[n] = sinc[n] {1[n+5]-1[n-5]}
f2[n] = 1-rect[n]
f3[n] = 1[n]-1[n-5]
write a programm in matlab in which you will check the following proprieties:
1)sinc[n]:=sin(phi*n)/phi*n;
2)(f1*f2)[n] = (f2*f1)[n];
3)f1[n]*{ f2[n] + f3[n] } = f1[n]*f2[n] + f1[n]*f3[n];
4)(f1*delta)[n] = (delta*f1)[n] = f1[n];
I'm really really grateful for any tips/ideal on how to solve this problem. :)
sinc[n]:=sin(phi*n)/phi*n;
That certainly isn't Matlab syntax, and the ; at the end makes it not look much like a question either. Anyway, you have two options. Either plot the functions to visually assess equivalence or else check the vectors. I'll demonstrate with this one, then you can try for all the others.
Firstly you need to make a sample n vector which will be your domain over which to test equivalence (i.e. the x values of your plot). I'm going to arbitrarily choose:
n = -10:0.01:10;
Also I'm going to assuming by phi you actually meant pi based on the Matlab definition of sinc: http://www.mathworks.com/help/signal/ref/sinc.html
So now we have to functions:
a = sinc(n);
b = sin(n)./n;
a and b are now vectors with a corresponding "y" value for each element of n. You'll also notice I used a . before the /, this means element wise divide i.e. divide each element by each corresponding element rather than matrix division which is inversion followed by matrix multiplication.
Now lets plot them:
plot(n, a, n, b, 'r')
and finally to check numerical equivalence we could do this:
all(a == b)
But (and this is probably a bit out of scope for your question but important to know) you should actually never check for absolute equivalence of floating point numbers like that as you get precision errors due to different truncations in the inner calculations (because of how your computer stores floating point numbers). So instead it is good practice to rather check that the difference between the two numbers is less than some tiny threshold.
all((a - b) < 0.000001)
I'll leave the rest up to you

syntax of Iteration in K-mean Clustering With MATLAB [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
i have a problem with using the number of iteration in MATLAB.
i use this code but there is no change in the number of iteration:
Edit
this is my code...
clc;clear;close all;diary temp;
cluster_data=unifrnd(-3,+3,[100 2]);
optns = statset('MaxIter',500);
[idx,ctrs]=kmeans(cluster_data,3,'dist','city', ...
'display','iter','replicate',100,'options',optns);
figure
plot(cluster_data(idx==1,1),cluster_data(idx==1,2),'r.','MarkerSize',14)
hold on
plot(cluster_data(idx==2,1),cluster_data(idx==2,2),'b.','MarkerSize',16)
plot(cluster_data(idx==3,1),cluster_data(idx==3,2),'g.','MarkerSize',18)
plot(ctrs(:,1),ctrs(:,2),'kx',...
'MarkerSize',12,'LineWidth',2)
plot(ctrs(:,1),ctrs(:,2),'ko',...
'MarkerSize',12,'LineWidth',2)
legend('Cluster 1','Cluster 2','Cluster 3','Centroids',...
'Location','NW')
fid=fopen('temp');
dat=textscan(fid,'%s');
fclose(fid);
delete temp
dat=dat{1};
i1=find(~cellfun('isempty',strfind(dat,'sum')));
ie=find(~cellfun('isempty',strfind(dat,'iterations')));
i1=i1(1)+1;
Nd=str2num(dat{ie(1)-1});
ie=Nd*4+i1-1;
dat=reshape(str2num(strvcat(dat{i1:ie})),4,Nd)';
iter = dat(:,1) % <-- iterations
sm = dat(:,4) % <-- sum
figure
plot(iter,sm)
the issue is the number of iteration... how can i really increase the number of iteration?
however i increase the 'Maxiter' but no change appear.
You should use statset to change the number of iterations:
optns = statset('MaxIter',500);
And the call kmeans like this:
[idx,ctrs]=kmeans(cluster_data,3,'dist','city', ...
'display','iter','replicate',100,'options',optns);
Comment
The documentation for kmeans states:
MaxIter - Maximum number of iterations allowed. The default is 100.
So if you change the value of MaxIter it should be a number different from 100.