# Matlab function (M-File) that will create & play a sinusoidal waveform

• makovx
In summary, the conversation is about creating a MATLAB function that can generate and play a sinusoidal waveform with given parameters, and save it as a .wav file with a specified filename. The function must follow a specific format and is used to produce a low frequency tone similar to the dial tone of a telephone. The code for the function is provided and can be used by those who are new to using MATLAB.
makovx
I am trying to create a MATLAB function (M-File) that will create & play a sinusoidal waveform with the given amplitude (A), frequency (f), sampling rate (Fs), and time span (tspan). the function must produce a .wav file with a specified filename.

the MATLAB Function Format must be:

function mytone(A,f,Fs,tspan,filename)

I have a sample problem:
the command mytone(0.4,300,8000,3,'threehundred.wav') creates and plays a sinusoid x(t)=0.4sin(2*pi*300t) for 3 secs. the sampling rate is 8000 Hz. the resulting tone is saved with a filename threehundred.wav.

after executing the command, you must hear a low frequency tone similar to the dial tone of telephone.

I am really a newbie in using MATLAB. I hope somebody can help me.

Here is the code:function mytone(A,f,Fs,tspan,filename)% Create a time vectort = 0 : 1/Fs : tspan;% Create a sinusoidal waveformx = A * sin(2*pi*f*t);% Create a .wav fileaudiowrite(filename, x, Fs);% Play the tonesound(x, Fs);end

I am happy to provide a response to your request for a MATLAB function that creates and plays a sinusoidal waveform. The function you have described is a great way to generate and save a sinusoidal waveform with specific parameters.

Here is an example of how you can create the MATLAB function you need:

% Define the function with the given inputs
function mytone(A,f,Fs,tspan,filename)

% Create the time vector based on the sampling rate and time span
t = 0:1/Fs:tspan;

% Generate the sinusoidal waveform using the amplitude and frequency inputs
x = A*sin(2*pi*f*t);

% Save the waveform as a .wav file with the specified filename
audiowrite(filename,x,Fs);

% Play the waveform using the sound function
sound(x,Fs);

% End of the function

To use this function, you can simply call it with the desired inputs, such as mytone(0.4,300,8000,3,'threehundred.wav'). This will generate a sinusoidal waveform with an amplitude of 0.4, a frequency of 300 Hz, a sampling rate of 8000 Hz, and a duration of 3 seconds. The resulting tone will be saved as a .wav file with the filename "threehundred.wav" and will also be played for you to hear.

I hope this helps you in creating your MATLAB function for generating and playing sinusoidal waveforms. Remember, as a scientist, it is important to always test and validate your code to ensure it is producing the desired results. Happy coding!

## 1. What is a Matlab function (M-File)?

A Matlab function, also known as an M-File, is a file that contains a set of instructions written in the Matlab programming language. These instructions can be executed to perform a specific task or calculation.

## 2. How can I create a sinusoidal waveform using a Matlab function?

To create a sinusoidal waveform using a Matlab function, you can use the built-in function "sin" and specify the desired frequency, amplitude, and duration of the waveform. You can also use the "plot" function to visualize the waveform.

## 3. Can I customize the parameters of the sinusoidal waveform in the Matlab function?

Yes, you can customize the frequency, amplitude, and duration of the sinusoidal waveform in the Matlab function. You can also add additional parameters such as phase shift or frequency modulation to create more complex waveforms.

## 4. How can I play the sinusoidal waveform created by the Matlab function?

To play the sinusoidal waveform, you can use the "sound" function in Matlab. This function will convert the waveform into an audio signal that can be played through your computer's speakers.

## 5. Can I save the sinusoidal waveform as an audio file using the Matlab function?

Yes, you can save the sinusoidal waveform as an audio file using the "audiowrite" function in Matlab. This function allows you to specify the file format and location where you want to save the audio file.

Replies
2
Views
12K
Replies
1
Views
5K
Replies
3
Views
48K
Replies
1
Views
3K
Replies
1
Views
10K
Replies
2
Views
25K
Replies
4
Views
13K