Plotting audio signal with number of samples
Mostra commenti meno recenti
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 Commenti
Image Analyst
il 12 Dic 2021
Do you have to extract 5000 samples (chunks of the waveform) or just one sample with 5000 elements (5000 time points)?
If you need 5000 chunks, how long are these time samples/chunks? The same length (like 1 second), or random lengths (random number of elements) like 0.1 seconds, 1.3 seconds, 3.2 seconds, etc.? And what about the starting point for the extracted sample? Are they chosen sequentially, or are the starting points randomly selected?
If you're just extracting one time period, and you want 5000 elements to be interpolated over that time period, then what is the starting and ending time of the extracted waveform? From element #1 to the very end? Or is it some smaller chunk of the full length original waveform?
AJ
il 12 Dic 2021
Walter Roberson
il 12 Dic 2021
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
il 12 Dic 2021
Risposta accettata
Più risposte (2)
Chunru
il 12 Dic 2021
% 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
il 15 Giu 2023
0 voti
% 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]);
Categorie
Scopri di più su Audio I/O and Waveform Generation in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!