I'm trying to perform STFT on an audio file. I need to get the fft of each window.
I used the follwing code.
[wave,fs] = wavread('40.wav');
w_length = 1024;
for v = 1:w_length:length(wave)
data_sub = wave(v:v+w_length);
subsection_fft = fft(data_sub);
figure(1)
plot(subsection_fft)
end
But i get the following error.
??? Index exceeds matrix dimensions.
Error in ==> chk at 7
data_sub = wave(v:v+w_length);
Can you tell me what I can do to rectify this.
As the error message says, you are trying to access a position in wave tat does not exist.
See this example:
a = rand(7,1);
step = 4;
1:step:7
ans =
1 5
when v = 5, you will try to access position v:v+step, i.e. 5 to 9, but a is only defined up to 7 elements.
In your case, wave is defined up to length(wave), but on the last iteration you will go out of bounds.
To avoid it, on approach would be to sample the end sequences and subtract the length of the sequence:
pos = (1+w_length:w_length:length(wave))-w_length
for v = pos
% do stuff
end
However, you will be left with some unprocessed part which you will have to do outside of the loop as last iteration.
Related
I'm using the following code of "Computed tomography-based volumetric tool for standardized measurement of the maxillary sinus" article to measure the volume of the maxillary sinus in DCOM images:
% Read images
clear all
close all
[filename, pathname] = uigetfile('*','Select CT exam (all slices)','MultiSelect','on');
num= length(filename);
bbbb=1;
step=input('Step of image reading: \')
for aaaa = 1:step:num
xinfo=dicominfo([pathname,char(filename(aaaa))]);
pxsp=cat(2,xinfo.PixelSpacing);
x=dicomread([pathname,char(filename(aaaa))])+cat(2,xinfo.RescaleIntercept);
k=x;
k = im2bw(k,0.49);
k = imfill(k,'holes');
cc = bwconncomp(k);
stats = regionprops(cc,'Area');
A = [stats.Area];
[~,biggest] = max(A);
k(labelmatrix(cc)~=biggest) = 0;
x(k~=1)=-2000;
masccranio(:,:,bbbb)=k;
cranio(:,:,bbbb)=x;
cranio_full(:,:,bbbb)=x;
bbbb=bbbb+1;
end
at the first, we don't have any idea about the reading step input at the beginning, please help on that if you can. Our second problem is when we run the code we get the following error:
Error using ~=
Matrix dimensions must agree.
Error in Quant (line 32)
k(labelmatrix(cc)~=biggest) = 0;
I'm using Matlab 2019b and as I know this code is for 2013. Any help is appreciated.
I expect you want biggest to represent the biggest value of all region areas in this line:
k(labelmatrix(cc)~=biggest) = 0;
In that case, your problem is likely that A is not a vector, and therefore biggest is not a scalar. This causes the operation to fail as the ~= operator only works on matrices of the same size or with scalars.
I'm trying to perform STFT on an audio file. I need to get the fft of each window.
I used the follwing code.
[wave,fs] = wavread('40.wav');
w_length = 1024;
for v = 1:w_length:length(wave)
data_sub = wave(v:v+w_length);
subsection_fft = fft(data_sub);
figure(1)
plot(subsection_fft)
end
But i get the following error.
??? Index exceeds matrix dimensions.
Error in ==> chk at 7
data_sub = wave(v:v+w_length);
Can you tell me what I can do to rectify this.
As the error message says, you are trying to access a position in wave tat does not exist.
See this example:
a = rand(7,1);
step = 4;
1:step:7
ans =
1 5
when v = 5, you will try to access position v:v+step, i.e. 5 to 9, but a is only defined up to 7 elements.
In your case, wave is defined up to length(wave), but on the last iteration you will go out of bounds.
To avoid it, on approach would be to sample the end sequences and subtract the length of the sequence:
pos = (1+w_length:w_length:length(wave))-w_length
for v = pos
% do stuff
end
However, you will be left with some unprocessed part which you will have to do outside of the loop as last iteration.
When I run this code that I've written to simulate a heat flow model in MATLAB i get an error that says 'Subscript indices must either be real positive integers or logicals.' I think this is probably something to do with my linspace command generating a different type of variable not integers and so it's not working properly but I'm not sure how to amend my script to correct for this.
Cp = 400;
p = 8960;
k = 400;
a = k/(p*Cp);
dt = 0.01;
dx = sqrt(5*a*dt); %% 5 as 1/5 is smaller than 1/4 for stability
T = zeros(20000,10000);
for x = linspace(1,10000,10000);
T(x,:) = 1000;
end
for x = linspace(10001,20000,10000);
T(x,:) = 25;
end
for t = linspace(1,10000,10000);
for x = linspace(1,20000,20000);
T(x,t+1) = T(x,t)+a*dt*((T(x-1,t)-2*T(x,t)+ T(x+1,t))/(dx*dx));
end
end
The line that blows up is:
T(x,t+1) = T(x,t)+a*dt*((T(x-1,t)-2*T(x,t)+ T(x+1,t))/(dx*dx));
Specifically T(x-1,t) triggers the error because x starts as 1, hence x - 1 = 0 and 0 is not a valid index.
On a more general Matlab coding note, I would write x = 1:10000 instead of x = linspace(1,10000,10000), but this is not causing the error. Note that I'm only addressing the Matlab error message. I have no idea whether your overall code works.
I'm new to matlab.
I wrote a function.
when i run my function in workspace at the first FOR i get this error :
??? Attempted to access XX(461,:); index out of bounds because
size(XX)=[460,440,3].
Error in ==> FOH_Zoom at 10
XX(j,:)=(XX(2*j-1,:)+XX(2*j+1,:))/2;
and this is my function :
function XX = FOH_Zoom(img)
X = ones(size(img)); %make a matrix of ones as size as image
XX=imresize(X,2); %make size of matrx double
[a,b]=size(X); %get the size of matrix
XX(1:2:end,1:2:end)=img(:,:); % fill odd rows and columns with original image data
[m,n]=size(XX);
for j=1:m-1
XX(j,:)=(XX(2*j-1,:)+XX(2*j+1,:))/2;
end
for i=1:n
XX(:,i)=(XX(:,2*i-1)+XX(:,2*i+1))/2;
end
imshow(XX); % show image
title([num2str(m),' *** ',num2str(n)]);
end
what is the problem ,do you think?
any help really appriciate
regards.
Your XX is 3D, so you have to use size like this,
[m,n,~]=size(XX);
Because:
XX=zeros(460,440,3);
[m,n]=size(XX)
m = 460
n = 1320
While,
[m,n,~]=size(XX)
m = 460
n = 440
Second Problem
in Line 10 you have,
XX(j,:)=(XX(2*j-1,:)+XX(2*j+1,:))/2;
When m = 231 then 2xm-1 = 461, and you will get the error out of bound,
I don't know what you want to do, but changing for j = 1 : m/2 and for i = 1 : n/2 would help.
Hey guys I am trying to find the Power Spectral Density of a .wav signal i recorded which is essentially a sine immersed in noise. The function that i have written is supposed to take records all of 1024 points in length and use it to find the Gxx of the signal by finding Gxx per record and then adding them and dividing them by the number of records better explained in the algorithm below:
a. Step through the wav file and extract the first record length (e.g. 1 to 1024 points). (Note that the record length is your new āNā, hence the frequency spacing changes in accordance with this, NOT the total length of the wav file).
b. Perform the normal PSD function on this record.
c. Store this vector.
d. Extract the next 1024 points in the wav file (e.g. 1025:2048) and perform PSD on this record.
e. Add this to the previously stored record and continue through steps c to e until you reach the end of your wav file or the total number of records you want. (Remember that total records*record length must be less than the total length of the wavfile!)
f. Divide the PSD by the number of averages (or number of records).
This is your averaged PSD
The function I created is as follows:
%Function to plot PSD
function[f1, GxxAv] = HW3_A_Fn_811003472_RCT(x,fs,NumRec)
Gxx = 0;
GxxAv = 0;
N = 1024;
df = fs/N;
f1 = 0:df:fs/2;
dt = 1/fs;
T = N*dt;
q = 0;
e = 1;
for i = 1:NumRec;
for r = (1+q):(N*e);
L = x(1+q:N*e);
M = length(L);
Xm = fft(L).*dt;
aXm = abs(Xm);
Gxx(1)=(1/T).*(aXm(1).*aXm(1));
for k = 2:(M/2);
Gxx(k) = (2/T) *(aXm(k).*(aXm(k)));
%Gxx = Gxx + Gxx1(k);
end
Gxx((M/2)+1)= (1/T)*(aXm((M/2)+1)).*(aXm((M/2)+1));
q = q+1024;
e = e+1;
%Gxx = Gxx + Gxx1((M/2)+1);
end
GxxAv = GxxAv + Gxx;
%Gxx = Gxx + Gxx1;
end
GxxAv = GxxAv/NumRec;
And the code I used to call this function is as follows:
[x,fs] = wavread('F:\Final\sem1Y3\Acoustics\sinenoise5s.wav');
[f1,GxxAv] = HW3_A_Fn_811003472_RCT(x,fs,100); %where 100 is the number of records to generated
plot(f1,GxxAv)
xlabel ('Frequency / Hz', 'fontsize', 18)
ylabel ('Amplitude Squared per Frequency / WU^2/Hz', 'fontsize', 18)
title ('Plot of the single sided PSD, using Averaging', 'fontsize', 18)
grid on
When Trying to plot this graph the following error was observed:
??? Index exceeds matrix dimensions.
Error in ==> HW3_A_Fn_811003472_RCT at 19
L = x(1+q:N*e);
Error in ==> HW3_A_3_811003472_RCT at 3
[f1,GxxAv] = HW3_A_Fn_811003472_RCT(x,fs,100); %where 100 is the number of records to generated
I am not sure how to fix it and i have tried many different methods but still i get this error. I am not too familiar with Matlab but all I really want to do for line 19 is to go like:
x(1:1024), x(1025:2048), x(2049:3072), x(3072:4096)...etc to 100 records
Any ideas??? Thanks
This is obviously homework, so I am not gonna do your work for you. But there quite some things wrong with your code. Start by fixing all of those first:
Use more appropriate function names, homework123 is not a good name to describe what the function does.
Use more appropriate variable names. More standard in this context would be nfft instead of N and n_average instead of NumRec. I don't care about the exact thing you use, but it should describe exactly what the variable does.
Your error message clearly hints that you are trying to index x in some illegal way. Start with making a loop that just prints the right indices (1..1024, 1025..2048, ...) and make sure it follows your instruction E. Only when this works as expected add the rest of the code.
you use a triple-nested for-loop. You only need a single for-loop or while-loop to solve this problem.