I have a problem while ploting this code.

1 visualizzazione (ultimi 30 giorni)
Essa Zulfikar Salas
Essa Zulfikar Salas il 28 Giu 2022
Risposto: VINAYAK LUHA il 28 Giu 2022
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.

Risposte (1)

VINAYAK LUHA
VINAYAK LUHA il 28 Giu 2022
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');

Categorie

Scopri di più su Audio I/O and Waveform Generation in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by