I have a problem while ploting this code.

3 ビュー (過去 30 日間)
Essa Zulfikar Salas
Essa Zulfikar Salas 2022 年 6 月 28 日
回答済み: VINAYAK LUHA 2022 年 6 月 28 日
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 件)

VINAYAK LUHA
VINAYAK LUHA 2022 年 6 月 28 日
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');

カテゴリ

Help Center および File ExchangeMeasurements 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