Plotting audio signal with number of samples

조회 수: 186 (최근 30일)
AJ
AJ 2021년 12월 12일
답변: philip 2023년 6월 15일
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
Walter Roberson
Walter Roberson 2021년 12월 12일
Why would you need to change the x axes? We showed you how to create the correct time vector -- at least for the case where you start from sample #1 of the file and that sample is intended to be time 0.
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]')

추가 답변 (2개)

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]')

philip
philip 2023년 6월 15일
% Read the .wav file
[y, fs] = audioread('your_file.wav');
% Extract the first 5000 samples
samples = y(1:5000);
% Create the corresponding time axis
t = (0:length(samples)-1) / fs;
% Plot the signal
plot(t, samples);
xlabel('Time (s)');
ylabel('Amplitude');
title('Plot of 5000 time samples');
% Optionally, you can adjust the figure size for better visibility
set(gcf, 'Position', [100, 100, 800, 400]);

카테고리

Help CenterFile Exchange에서 Audio I/O and Waveform Generation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by