Question: Examine the file Prob_1b.m. Write the function fn_detectFrequency(...). This function will read the values saved in arrayWithLatestValues and detect the frequency of the signal using
Examine the file Prob_1b.m. Write the function fn_detectFrequency(...). This function will read the values saved in arrayWithLatestValues and detect the frequency of the signal using the following algorithm:
1. Beginning with the last value saved in arrayWithLatestValues, begin scanning backwards through the array searching for a falling edge in the data (i.e. a value less than threshold at one point in time, then greater than or equal to threshold at the previous point in time). Use a suitable value for threshold.
2. If a falling edge event is detected, store the index at which the event occurs.
3. Ensure that, as the function continues to scan backwards through the array, the data stays above threshold for a number of samples at least equal to hysteresisWindowLength. a. If it does not, reject the threshold event previously detected and return to step 2. b. If it does remain above the threshold for an adequate number of samples, proceed to step 4.
4. When a second falling edge event is detected, store the index at which it occurs.
5. Ensure that the second falling edge event satisfies the hysteresis condition as well. a. If it does not, reject the event and return to step 4. b. If it does remain above the threshold, proceed to step 6.
6. Assume that the time between falling edge events is the signal period. Use this to calculate the apparent signal frequency.
7. Repeat steps 2-6 for the number of periods given by numberOfCyclesToAverage.
8. Average the frequencies. This is the value fn_detectFrequency(...) will return.
%%%% Problem 1(b) clear all; clc; global arraySize arrayWithLatestValues samplingRate;
arraySize = 500; arrayWithLatestValues = zeros(1,arraySize); samplingRate = 50; % in sps (sampling rate of the ADC) f = 2.8; rateOfDetectingFrequency = 1; % Number of times/sec you'll run "fn_detectFrequency" to compute the frequency
threshold = 2.6; hysteresisWindowLength = 1; numberOfCyclesToAverage = 1;
%%% Create source signal: maxTime = 10; % in seconds numberOfSamples = round( maxTime * samplingRate );
noise_amplitude = 1000;
simulated_raw_ADC_outputs = 2048 + 1500 * cos(2*pi*f*[0:numberOfSamples-1]*(1/samplingRate)) + noise_amplitude*rand(1,numberOfSamples);
index2 = 1; for index1 = 1:numberOfSamples
newValue = (3.3/4095) * simulated_raw_ADC_outputs(index1);
fn_updateArray(newValue); %% WRITE THIS FUNCTION IN A SEPARATE .m FILE
if mod( index1 , round(samplingRate/rateOfDetectingFrequency) ) == 0 currentFrequency = fn_detectFrequency(threshold,hysteresisWindowLength,numberOfCyclesToAverage ); %% WRITE THIS FUNCTION IN A SEPARATE .m FILE detectedFreqArray(:,index2) = [currentFrequency;index1]; index2 = index2 + 1; end end
figure; subplot(2,1,1); plot((3.3/4095) * simulated_raw_ADC_outputs); xlabel('Sample Index'); ylabel('Input Voltage'); axis([1 numberOfSamples 0 3.3]); grid on; title(['Frequency = ' num2str(f) ' , F_s = ' num2str(samplingRate)]);
subplot(2,1,2); plot(detectedFreqArray(2,:),detectedFreqArray(1,:),'ro'); xlabel('Sample Index'); ylabel('Detected Frequency'); axis([1 numberOfSamples 0 ceil(max( detectedFreqArray(1,:) )+1)]); grid on;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
