Plotting audio signal with number of samples

120 ビュー (過去 30 日間)
AJ
AJ 2021 年 12 月 12 日
コメント済み: AJ 2021 年 12 月 12 日
So I have a .wav file and I need to plot 5000 time samples of it. The .wav file is sampled at 44.1 kHz (which would be my fs).
I initially was able to plot my signal, but then realized I needed to do it over 5000 samples. I tried doing it, but would get the error "Vectors must be the same length" when I go to plot.
Original Code
[x,fs]=audioread('Song.wav');
t=linspace(0,length(x)/fs,length(x));
plot(t,x)
xlabel('time')
ylabel('x[n]')
New Code (which I thought I would only need to change my linspace, but it doesn't seem to like it)
n = 5000;
[x,fs]=audioread('Song.wav');
t=linspace(0,length(x)/fs,n);
plot(t,x)
xlabel('time')
ylabel('x[n]')
Appreciate any help/guidance!
  4 件のコメント
AJ
AJ 2021 年 12 月 12 日
I see. Just needed to think it through a little more. Appreciate the help.

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2021 年 12 月 12 日
n = 5000;
[x,fs]=audioread('Song.wav');
idx = 1:n;
subset = x(idx,:);
t = (idx-1)./fs;
plot(t,subset)
xlabel('time')
ylabel('x[n]')

その他の回答 (1 件)

Chunru
Chunru 2021 年 12 月 12 日
% Read all data
[x,fs]=audioread('Song.wav');
t = (0:length(x)-1)/fs;
%t=linspace(0,length(x)/fs,length(x));
plot(t,x)
xlabel('time')
ylabel('x[n]')
n = 5000;
plot(t(1:n),x(1:n))
xlabel('time')
ylabel('x[n]')

Community Treasure Hunt

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

Start Hunting!

Translated by