Im new to Matlab and was having trouble doing this question. Any help would be much appreciated.

2 vues (au cours des 30 derniers jours)
An engineer has recorded a sound signal from a microphone, and sampled it; these values are stored in a vector. The units of each sample is volts. The microphone was not on at all times, and, therefore, some data samples below a certain threshold are considered to not valid data samples; samples greater than or equal to the threshold are valid. The en- gineer would like to know the average voltage of the sound signal, given by, sum of valid samples/number of valid samples and the fraction of valid data samples, given by, number of valid samples/total number of samples . If the number of valid samples is 0, then the function must return 0 for both the average voltage and the fraction of valid data points.
Write a function, computeAverageVoltage with the following signature.
function [average_voltage, frac_valid] =
computeAverageVoltage(v, voltage_threshold)
Inputs:
v - a vector that contains the values of all the voltage samples (in volts) voltage_threshold - threshold voltage (in volts)
Output:
average_voltage - average voltage computed over valid samples frac_valid - fraction of valid samples
Note: You are expected to use an appropriate loop to iterate through the vector and compute the required values, rather than using MATLAB built-in functions such as, sum, average and vector computations
  7 commentaires
Steven Lord
Steven Lord le 13 Sep 2018
Hint: the problem statement said you weren't allowed to use sum. However, it did not say that the plus operator + was forbidden.
Rather than using length (which will give the number of elements of a vector but would give you something different if you called it on a matrix) or size to determine the number of elements, there is a function that gives you exactly that information for vectors, matrices, 17-dimensional arrays, etc. Search the documentation for "number of elements" to find it. [IMO that's a good habit to develop: if you're not sure if there's a function to do what you need, think of a couple of keywords related to your task and search the documentation.]
Saad
Saad le 13 Sep 2018
im assuming that you are referring to the numel operation. How would i select certain elements from a given matrix that satisfy a particular property and also count the number of elements that satisfy the property?

Connectez-vous pour commenter.

Réponses (2)

Image Analyst
Image Analyst le 13 Sep 2018
Hint:
meanOfGoodValues = mean(v > voltage_threshold);
No loop needed.
  6 commentaires
Saad
Saad le 13 Sep 2018
"you'd need to check each value one at a time, not the whole vector, and keep track of the sum and count separately, then compute the mean after the loop is done". How would i do this exactly?
Image Analyst
Image Analyst le 14 Sep 2018
if (v(i) > voltage_threshold)
checked one element at a time, v(i), which is the i'th element.
if (v > voltage_threshold)
like you had, checks the whole vector and gives not one true or false value, but a whole array of them - a true or false value for every element in the vector so you get a binary/logical/boolean vector, not a single value.

Connectez-vous pour commenter.


Robert Allison
Robert Allison le 14 Sep 2018
lol ESD2

Catégories

En savoir plus sur Audio Processing Algorithm Design dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by