Hi Michael,
I understand that you are trying to plot an audio file for 20 milliseconds which is recorded in the file itself.
To display the frequency in kHz, you would typically perform a Fourier transform on the audio signal to convert it from the time domain to the frequency domain. Here's how you can modify your code to do this:
% Record and save audio
recObj = audiorecorder;
Fs = 8000;
filename = 'myAudioData.wav';
disp('Start speaking.')
recordblocking(recObj, 10);
disp('End of Recording.');
doubleArray = getaudiodata(recObj);
audiowrite(filename,doubleArray,Fs);
% Read audio
[x,Fs] = audioread('myAudioData.wav');
[m,n]=size(x);
dt=1/Fs;
t=dt*(0:m-1);
% Select the time segment
idx = (t>=1.030) & (t<1.032);
selected_t = t(idx);
selected_x = x(idx,:);
% Plot the time domain signal
subplot(2,1,1);
plot(selected_t, selected_x);
title('Time Domain Signal')
xlabel('Time (s)')
ylabel('Amplitude')
% Perform the Fourier transform
Y = fft(selected_x);
m_sel = length(selected_x);
P2 = abs(Y/m_sel);
P1 = P2(1:m_sel/2+1);
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(m_sel/2))/m_sel;
% Plot the frequency domain signal
subplot(2,1,2);
plot(f/1000, P1)
title('Frequency Domain Signal')
xlabel('Frequency (kHz)')
ylabel('|P1(f)|')
Refer to the following MATLAB Documentation to know more about Fast Fourier Transform (‘fft’) function:
Hope this helps.