find the dc value of a vector with an unknown sampling rate in matlab

To find the DC value of a vector in Matlab without knowing its sampling rate, you can simply use the mean function to calculate the average of all the values in the vector.

main.m
% Sample vector
x = [1 2 3 4 5];

% Calculate DC value
dc = mean(x);
69 chars
6 lines

The mean function calculates the sum of all the elements in the vector and divides it by the number of elements in the vector, giving you the DC value.

If you have a signal sampled at a certain rate and you want to find the DC value of a portion of that signal, you can use the dcblock function to remove any AC components in the signal and then calculate the mean.

main.m
% Sample signal sampled at 100 Hz
fs = 100; % Hz
t = 0:1/fs:1-1/fs; % Time vector
x = sin(2*pi*5*t) + sin(2*pi*10*t) + 1; % Signal

% Remove any DC offset and calculate DC value
dc = mean(dcblock(x));
201 chars
8 lines

The dcblock function, available in the Signal Processing Toolbox, removes any DC offset in the signal by high-pass filtering it with a cutoff frequency of 0.005 Hz, and outputs the filtered signal.

gistlibby LogSnag