how to redefine function as imported audio when plotting a frequency spectrum

1 次查看(过去 30 天)
im making a function where the user inputs an audio vector and the frequency spectrum gets plotted.
input1 = an array consisting of the signal that will be plotted, or the filename of an audio file.
sampleRate = asks user to input a sampleRate
input3 = makes a plot
  1. if the user inputs the filename of an audio file, how can i redefine input1 as the audio from the file and instead not a given array?
this is what i have
function [output] = mainfreq(input1, sampleRate, input3)
if strcmpi(input1, '.wav')
input1 = audioread(input1);

回答(1 个)

Sai Kiran
Sai Kiran 2022-12-23
Hi,
To read the audio data from a file and assign it to the 'input1' variable, you can use the 'audioread' function. This function takes the filename of the audio file as input and returns the audio data and the sample rate as output.
Here is how you can modify your code to read the audio data from a file and assign it to the 'input1' variable
function [output] = mainfreq(input1, sampleRate, input3)
if ischar(input1)
[input1, sampleRate] = audioread(input1);
end
This code checks if the 'input1' variable is a string (i.e., if it is the filename of an audio file). If it is a string, it reads the audio data from the file using audioread and assigns it to the input1 variable. The sample rate is also read from the file and assigned to the 'sampleRate' variable.
I hope this helps!

Community Treasure Hunt

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

Start Hunting!

Translated by