I have a problem while ploting this code.

조회 수: 10 (최근 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 CenterFile 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