Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please help with the following Matlab code. Please keep it simple! Thanks!! A2_P

ID: 2268007 • Letter: P

Question

Please help with the following Matlab code. Please keep it simple! Thanks!!

A2_Prob_2a.m

clear all; clc;
function[
global arraySize arrayWithLatestValues ;
arraySize = 7;
arrayWithLatestValues = zeros(1,arraySize);

%%% Create source signal (NOTE: for real-time data acquisition, the following
%%% block will not be required since the data is generated/processed in the analog domain)
samplingRate = 50; % in sps (sampling rate of the ADC)
maxTime = .24; % in seconds
numberOfSamples = round( maxTime * samplingRate );

simulated_raw_ADC_outputs = randi(4095,1,numberOfSamples); % for older Matlab versions use: randint(1,numberOfSamples,4095);   

for index1 = 1:numberOfSamples

% read one sample at a time (NOTE: for real-time data acquisition, the following line will be
% replaced by something like "newValue = (3.3/4095.0) * analogRead( ... )" )
newValue = (3.3/4095) * simulated_raw_ADC_outputs(index1);

% save the sample in the array called "arrayWithLatestValues" ;
% the last value in the array is the latest ; shift other values;
% discard values prior to the latest "arraySize" values

fn_updateArray(newValue); %% WRITE THIS FUNCTION IN A SEPARATE .m FILE

disp(arrayWithLatestValues);
end

A2_Prob_2b.m

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;

Problem 2 In this problem, you will simulate an analog noisy periodic signal, sample the signal and convert it to a digital signal, and write a function to detect the frequency of the original signal You MUST attach all your codes with your report (a) [10 points] Examine the file "A2_Prob_2a.m". Write the function "fn_updateArray(newValue)". This function will save "newValue" at the end of the array (called "arrayWithLatestValues") containing the latest sampled values. Every time a new value is read, the older values will be shifted to the left and the oldest value will be discarded Run the program a few times and include screenshots of the command window in your report. (b) [50 points] Examine the file "A2_Prob_2b.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 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". If a falling edge event is detected, store the index at which the event occurs 1. 2. 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". 3. If it does not, reject the threshold event previously detected and return to step2 If it does remain above the threshold for an adequate number of samples, proceed to step 4 a. b. 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 wel If it does not, reject the event and return to step 4 If it does remain above the threshold, proceed to step 6 a. b. 6. Assume that the time between falling edge events is the signal period. Use this to calculate 7. 8. the apparent signal frequency Repeat steps 2-6 for the number of periods given by "numberOfCyclesToAverage" 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

Explanation / Answer

after simulation the problem has been taken as

clear all; clc;
function[
global arraySize arrayWithLatestValues ;
arraySize = 7;
arrayWithLatestValues = zeros(1,arraySize);

%%% Create source signal (NOTE: for real-time data acquisition, the following
%%% block will not be required since the data is generated/processed in the analog domain)
samplingRate = 50; % in sps (sampling rate of the ADC)
maxTime = .24; % in seconds
numberOfSamples = round( maxTime * samplingRate );

simulated_raw_ADC_outputs = randi(4095,1,numberOfSamples); % for older Matlab versions use: randint(1,numberOfSamples,4095);   

for index1 = 1:numberOfSamples

% read one sample at a time (NOTE: for real-time data acquisition, the following line will be
% replaced by something like "newValue = (3.3/4095.0) * analogRead( ... )" )
newValue = (3.3/4095) * simulated_raw_ADC_outputs(index1);

% save the sample in the array called "arrayWithLatestValues" ;
% the last value in the array is the latest ; shift other values;
% discard values prior to the latest "arraySize" values

fn_updateArray(newValue);   
disp(arrayWithLatestValues);
end

clear all; clc;
function[
global arraySize arrayWithLatestValues ;
arraySize = 7;
arrayWithLatestValues = zeros(1,arraySize);

%%% Create source signal (NOTE: for real-time data acquisition, the following
%%% block will not be required since the data is generated/processed in the analog domain)
samplingRate = 50; % in sps (sampling rate of the ADC)
maxTime = .24; % in seconds
numberOfSamples = round( maxTime * samplingRate );

simulated_raw_ADC_outputs = randi(4095,1,numberOfSamples); % for older Matlab versions use: randint(1,numberOfSamples,4095);   

for index1 = 1:numberOfSamples

% read one sample at a time (NOTE: for real-time data acquisition, the following line will be
% replaced by something like "newValue = (3.3/4095.0) * analogRead( ... )" )
newValue = (3.3/4095) * simulated_raw_ADC_outputs(index1);

% save the sample in the array called "arrayWithLatestValues" ;
% the last value in the array is the latest ; shift other values;
% discard values prior to the latest "arraySize" values
fn_updateArray(newValue);
disp(arrayWithLatestValues);
end

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);   

if mod( index1 , round(samplingRate/rateOfDetectingFrequency) ) == 0
currentFrequency = fn_detectFrequency(threshold,hysteresisWindowLength,numberOfCyclesToAverage );
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;