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
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.]
답변 (2개)
Image Analyst
2018년 9월 13일
Hint:
meanOfGoodValues = mean(v > voltage_threshold);
No loop needed.
댓글 수: 6
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Audio Processing Algorithm Design에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!