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