What is the purpose of this code?
2 views (last 30 days)
Show older comments
What is the purpose of this code? Sorry, I do not have the function for signal, but I hope the code is enpugh for you to answer the question.
a) Plot the correlation between the signal and 100 different sine waves.
b) Plot the crosscorrelation between the signal and 100 different sine waves.
c) Plot the autocorrelation between the signal and 100 different sine waves.
d) Plot the covariance between the signal and 100 different sine waves.
e) Plot the crossvariance between the signal and 100 different sine waves.
clear all;
close all;
clc;
% What is the purpose of this code?
load signal;
fs = 100;
t = (1:length(signal))/fs;
for i = 1:100
f(i) = 0.35*i;
x = sin(2*pi*f(i)*t);
rxy = xcorr(signal,x);
rmax(i) = max(rxy);
end
plot(f,rmax,'k');
Answers (1)
Luca Ferro
on 21 Feb 2023
line per line
load signal; %load signal
fs = 100;
t = (1:length(signal))/fs; %create linspace (the time axis)
for i = 1:100 %cycle 100 times
f(i) = 0.35*i; %constant which changes value linearly every cycle, see below
x = sin(2*pi*f(i)*t); %generates a different sine wave at every cycle, the difference is guaranteed by f
rxy = xcorr(signal,x); %calculates the correlation between signal and x (it's an array), x is the sine generated in the current cycle
rmax(i) = max(rxy); %stores the maximum value of the above calculated array in a second array
end
plot(f,rmax,'k'); %plots every maximum correlation value stored
basically every cycle it creates a sine wave, compares it to the given signal, finds the maximum correlation, stores it. Once the cycle is done it plots it.
0 Comments
See Also
Categories
Find more on Whos in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!