User-Defined Function Help?

조회 수: 2 (최근 30일)
William Boening
William Boening 2012년 11월 8일
Okay, so I've only had a tiny bit of programming in the past, and it was in HTML. So this is all new to me, and I'm not very good with it, and we're currently on a certain programming assignment and I'm lost on what to do.
To start it off, we are creating multiple vectors based on what the input arguments are, but I'll just copy and paste what the assignment says so you can get a better feel.
A) Instead of generating your audio vectors with fixed wave forms, frequencies and durations, in this lab, we write a user defined function with three arguments (wave form, frequency and duration) that returns an audio vector defined by these three arguments.
Our sampling rate is a fixed value of 20000.
1. The first argument: wave form
Your audio vector generator should be able to generate vectors in three different wave forms. For this argument, you should be able to pass a ‘string-typed’ argument to your function to define the wave form of the vector you are going to generate. The three possible values for this argument are ‘wedge’, squareand ‘sine’, and other value will be regarded as invalid and an error message should be printed in the command window to inform the user about this.
2. The second argument: frequency
For this argument, you should be able to pass the frequency to your function. Any value less than 20 or larger than 20k will be regarded as invalid since an audio signal that can be heard by human beings should be between 20 Hz and 20k Hz , and an error message should be printed in the command window to inform the user about this. This argument is used to define the frequency of your vector.
In your program, you need to calculate the number of samples in each period of the audio vector from the frequency and the fixed sampling rate (20000).
Sampling rate = 20000 Hz
Number of samples in one period = [sampling rate / frequency] *
* round to integer
Since the number of samples in each period has to be an integer, you need to round it (you can use ‘floor’, ‘fix’ or ‘ceil’ to do this).
3. The third argument: duration
For this argument, pass a value as the duration of your audio vector in seconds to your function. So if you pass 1.5, it means you will generate an audio vector which will last 1.5 seconds when you use ‘sound’ to play it at the fixed sampling rate of 20000.
Write code to accept values from 0.1 to 5 as valid values (so that your audio vector will have a proper length).
You need to use this ‘duration’ to calculate the length of your audio vector.
Length = duration * sampling rate
B) Think about how we convert a wedge wave form to a square wave form using a logical vector. Make sure that you understand the idea behind this. In your program, after you get your periodic vector, modify the amplitude of your vector to make the values in it range between 0 and 1.
C) Write a menu function in your program to give you three options after you generated your audio vector. One is ‘play’ and one is ‘plot’ and the other one is ‘exit’. Play it when the ‘play’ button is pressed and plot it if the ‘plot’ button is pressed and exit if the ‘exit’ button is pressed. Use title, xlabel, and ylabel in your figure. Use a ‘while loop’ to keep your menu there before you exit.
D) Use the tic/toc functions to compare the execution time in generating each of the 3 waveforms (the wedge, square, and sine waves). Plot your results as a bar graph.
And here is my code, or what I have of it;
function audio = audiogen(wave,freq,duration)
A = int16(ones(1,duration*20000));
A1 = A * freq;
W1=double(mod(n, A1));
W1_sq= (W1>20).*1;
W1_sin= sin(W1./40.*pi*4);
if strcmp(wave,sin)
audio=sound (W1_sin,20000);
elseif strcmp(wave,sq)
audio=sound (W1_sq,20000);
elseif strcmp(wave,sin)
audio=sound (W1,20000);
else
disp ('invalid input');
end
When I run this, it claims that I have an error on line 4, with not enough input arguments. I'm just wondering what I'm doing wrong, and what would be the correct steps to get this all figured out and solved.
Thanks.
  댓글 수: 3
Walter Roberson
Walter Roberson 2012년 11월 8일
You do not indicate which is line 4.
How are you invoking the function?
Jan
Jan 2012년 11월 8일
If line 4 is "W1=double(mod(n, A1));", I'd expect an error message concerning the unknown variable "n".

댓글을 달려면 로그인하십시오.

답변 (1개)

Akiva Gordon
Akiva Gordon 2012년 11월 8일
Please see the following related question/answer:

카테고리

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