I don't know what's the problem, can somebody help me, i don't know the exact length of myspeech/myrecord since it varies on how fast i read

 Accepted Answer

Maybe you meant to name the variable returned by getaudiodata "mySpeech" instead of "y":
% y = getaudiodata(recObj);
mySpeech = getaudiodata(recObj);

8 Comments

Thank u for answering, u are correct, but after that another error occured.
The second component of the parameter for axes must be strictly greater than the first component. Your code would have trouble if somehow max(f) is less than or equal to 200.
You're welcome!
Well, I'm not able to see the complete error message, but an error like that could happen if mySpeech has more than one channel. For example:
Fs = 8000;
mySpeech = rand(5*Fs,2); % 2 channels (columns) of random data, for 5 seconds at 8000 samples/second
m = length(mySpeech);
n = pow2(nextpow2(m));
y2 = fft(mySpeech,n);
f = (0:n-1)*(Fs/n);
mag = 2*abs(y2)/n;
figure(2)
plot(f,mag)
xlabel('Freq')
ylabel('Mag')
axis([200 max(f) 0 max(mag)])
Error using axis>LocSetLimits
Vector must have 4, 6, or 8 elements.

Error in axis (line 115)
LocSetLimits(ax(j),cur_arg,names);
I would comment-out or remove the axis(...) line until you know the results are correct and plotted correctly. Then come back and decide whether and how the axes limits need to be changed.
Good point about the number of channels... but when you use audiorecorder() with no parameters then the default number of channels is always 1.
"Your code would have trouble if somehow max(f) is less than or equal to 200."
@Walter Roberson: That was my first thought too, but
So implies
(assuming )
So that's not a problem when , which, for , is . So as long as there are at least 2 samples, .
Fs = 8000;
n = 2; % 2 samples OK
f = (0:n-1)*(Fs/n);
max(f)
ans = 4000
figure
xlim([200 max(f)])
Fs = 8000;
n = 1; % 1 sample -> error
f = (0:n-1)*(Fs/n);
max(f)
ans = 0
figure
xlim([200 max(f)])
Error using xlim
Limits must be a 2-element vector of increasing numeric values.
@Jane: Can you upload your variable mySpeech? You can save it to a mat file using
mySpeech = getaudiodata(recObj);
save('mySpeech.mat','mySpeech')
and then upoad the resulting mat file using the paperclip button.
Also, it may be helpful to see the complete error message, i.e., please copy all the red text from the command window and and paste it as text into a comment.
@Voss Thank u for answering, I already figure it out. I just want to ask, what can I add in my code if I want to know the frequency and magnitude of mySpeech. Like, the frequency and magnitude will be shown in the command window like this:
For frequency content you can use fft or see this for some options: https://www.mathworks.com/matlabcentral/answers/160059-finding-the-frequency-value-of-a-signal
For magnitude you can use abs(mySpeech) or mean(abs(mySpeech)).

Sign in to comment.

More Answers (1)

Hi Jane,
It is my understanding that the variable passed in the "fft" function is not of correct datatype. "fft" functions expects datatypes of either double or single or int8 or int16 or int32 or uint8 or uint16 or uint32 or logical type.
Note - It is not clear what value mySpeech variable contains.
Please refer to the following MathWorks documentation for more information on the “fft” function:
I hope this helps!
Regards
Binaya

Products

Release

R2023a

Asked:

on 14 Jan 2024

Commented:

on 15 Jan 2024

Community Treasure Hunt

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

Start Hunting!