Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help the code is in Matlab Examine the file A1_Prob_3b.m. Write the function fn_detectFrequency(...). This function will read the values saved in arrayWithLatestValues and

Please help the code is in Matlab

Examine the file A1_Prob_3b.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. Vary the signal frequency, threshold and SNR; plot your results and comment on these results. Show how changing the hysteresisWindowLength and numberOfCyclesToAverage can improve the accuracy of frequency detection, especially at higher SNRs.

and

%%%% BMEN 3350, Assignment 1: Problem 3(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 );

simulated_raw _ADC _outputs = 2048 + 1500 * cos(2*pi*f*[0:numberOfSamples-1]*(1/samplingRate)) + 1000*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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions