I have a problem while ploting this code.

1 view (last 30 days)
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.

Answers (1)

VINAYAK LUHA
VINAYAK LUHA on 28 Jun 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');

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!