calculate energy of signal x1 = cos(2*pi*fo*tvec*2^(-9/12)); in matlab

To calculate the energy of a signal x1, we need to square each sample in the signal and sum up all the squared samples. Here's the MATLAB code to calculate the energy of the given cosine signal:

main.m
fo = 440; % frequency of the cosine signal
fs = 44100; % sampling rate
dur = 1; % duration of the signal in seconds
tvec = 0:1/fs:dur-1/fs; % time vector
x1 = cos(2*pi*fo*tvec*2^(-9/12)); % cosine signal

% calculate energy of the signal
energy = sum(x1.^2);
259 chars
9 lines

In this code, we first define the frequency (fo), sampling rate (fs), duration (dur), and time vector (tvec) of the signal. Then, we generate the cosine signal (x1). Finally, we calculate the energy of the signal by squaring each sample in the signal (x1.^2) and summing up all the squared samples (sum(x1.^2)). The result is stored in the variable energy.

gistlibby LogSnag