Im new to Matlab and was having trouble doing this question. Any help would be much appreciated.
2 views (last 30 days)
Show older comments
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 Comments
Steven Lord
on 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.]
Answers (2)
Image Analyst
on 13 Sep 2018
Hint:
meanOfGoodValues = mean(v > voltage_threshold);
No loop needed.
6 Comments
Image Analyst
on 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.
See Also
Categories
Find more on Audio Processing Algorithm Design in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!