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

조회 수: 2 (최근 30일)
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
Steven Lord
Steven Lord 2018년 9월 13일
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 2018년 9월 13일
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?

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

답변 (2개)

Image Analyst
Image Analyst 2018년 9월 13일
Hint:
meanOfGoodValues = mean(v > voltage_threshold);
No loop needed.
  댓글 수: 6
Saad
Saad 2018년 9월 13일
"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 2018년 9월 14일
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.

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


Robert Allison
Robert Allison 2018년 9월 14일
lol ESD2

카테고리

Help CenterFile Exchange에서 Audio Processing Algorithm Design에 대해 자세히 알아보기

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by