I have a problem while ploting this code.

Fs = 4000;
Channels = 1;
bits = 16;
duration = 5;
t = 0:1/Fs:(length(x)-1)/Fs;
r = audiorecorder(Fs, bits, Channels);
disp('Recording Start')
recordblocking(r, duration);
disp('Recording Stop')
X = getaudiodata(r);
sound(X, Fs, bits);
filename = 'myvoice.wav';
audiowrite(filename, X, Fs);
n = length(X); F = 0:(n-1)*Fs/n;
Y = fft(X,n);
F_0 = (-n/2:n/2-1).*(Fs/n);
Y_0 = fftshift(Y);
AY_0 = abs(Y_0);
figure(1)
plot(t, X, 'LineWidth',1.5);
xlabel('time (sec)'); ylabel('Amplitude');
title('Time Domain Plot of the Recorded Signal');
figure(2)
plot(F_0), AY_0, 'LineWidth',1.5);
xlabel('Frequency (Hz)'); ylabel('Amplitude');
title('Frequency Domain Plot of the Recorded Signal');
Recording Start
Recording Stop
Error using plot
Vectors must be the same length.

回答(1 个)

Hi Essa ,
I couldn't reproduce the mentioned error as you haven't specified the value of 'x' hence code breaks before the plot function executes. Hope the follwing code solves your problem.
little explanation:
Since X will have 20,000 values (Fs*duration) ,I just stored the timestamps of each samples in t and plotten X vs t.
Fs = 4000;
Channels = 1;
bits = 16;
duration = 5;
t = (0:1/Fs:duration-1/Fs)';
r = audiorecorder(Fs, bits, Channels);
disp('Recording Start')
recordblocking(r, duration);
disp('Recording Stop')
X = getaudiodata(r);
sound(X, Fs, bits);
filename = 'myvoice.wav';
audiowrite(filename, X, Fs);
n = length(X); F = 0:(n-1)*Fs/n;
Y = fft(X,n);
F_0 = (-n/2:n/2-1).*(Fs/n);
Y_0 = fftshift(Y);
AY_0 = abs(Y_0);
figure(1)
plot(t,X, 'LineWidth',1.5);
xlabel('time (sec)'); ylabel('Amplitude');
title('Time Domain Plot of the Recorded Signal');
figure(2)
plot((F_0), AY_0, 'LineWidth',1.5);
xlabel('Frequency (Hz)'); ylabel('Amplitude');
title('Frequency Domain Plot of the Recorded Signal');

类别

帮助中心File Exchange 中查找有关 Measurements and Spatial Audio 的更多信息

产品

版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by