# Difficulty in Calculating Signal Frequency with an Offset

6 visualizaciones (últimos 30 días)
Raphael Brugger el 11 de Mayo de 2024
Editada: Raphael Brugger el 12 de Mayo de 2024
So I wrote a Matlab program that calculates and plots some key values of a signal. The signal is stored in a .csv file.
The problem is that the calculation of the frequency only works for signals that don't have an offset.
I would appreciate any kind of help since I can't seem to figure it out on my own.
Matlab program:
% Extract time and voltage columns
time = data(:, 1);
voltage = data(:, 2);
% Calculate peak value
peak_value = max(abs(voltage));
% Calculate arithmetic mean in mV
mean_value = mean(voltage) * 1000; % Conversion from V to mV
% Calculate rectified value
rectified_value = mean(abs(voltage));
% Calculate RMS (Root Mean Square) value
rms_value = rms(voltage);
% Calculate form factor
form_factor = rms_value / rectified_value;
% Calculate crest factor
crest_factor = peak_value / rms_value;
% Calculate sampling frequency (Assumption: uniformly sampled signal)
sampling_frequency = 1 / (time(2) - time(1));
% Calculate FFT (Fast Fourier Transform) of the signal
N = length(voltage);
Y = fft(voltage);
f = sampling_frequency*(0:(N/2))/N;
% Find the index of the maximum in the frequency domain
[~, max_index] = max(abs(Y(1:N/2+1)));
% Extract frequency at the maximum
frequency = f(max_index);
% Calculate period duration in ms
period_duration = 1/frequency * 1000; % Conversion from s to ms
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Paul el 11 de Mayo de 2024
Hi Raphael,
The first point in the FFT is always the sum of the elements of the input. When the signal includes the offset, the abs of the sum of the elements is larger than the peak of the FFT at the signal frequency.
% Extract time and voltage columns
time = data(:, 1);
voltage = data(:, 2);
figure
plot(time,voltage),grid
% Calculate peak value
peak_value = max(abs(voltage));
% Calculate arithmetic mean in mV
mean_value = mean(voltage) * 1000; % Conversion from V to mV
% Calculate rectified value
rectified_value = mean(abs(voltage));
% Calculate RMS (Root Mean Square) value
rms_value = rms(voltage);
% Calculate form factor
form_factor = rms_value / rectified_value;
% Calculate crest factor
crest_factor = peak_value / rms_value;
% Calculate sampling frequency (Assumption: uniformly sampled signal)
sampling_frequency = 1 / (time(2) - time(1));
% Calculate FFT (Fast Fourier Transform) of the signal
N = length(voltage);
Y = fft(voltage);
f = sampling_frequency*(0:(N/2))/N;
figure
plot(f,abs(Y(1:numel(f))),'-o')
xlim([0 1e4])
% Find the index of the maximum in the frequency domain
[~, max_index] = max(abs(Y(1:N/2+1)));
% Extract frequency at the maximum
frequency = f(max_index);
% Calculate period duration in ms
period_duration = 1/frequency * 1000 % Conversion from s to ms
period_duration = Inf
You can ignore the first point in the FFT or you can subtract the mean voltage before taking the fft
Y = fft(voltage-mean(voltage));
##### 3 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
Paul el 11 de Mayo de 2024
The mean is in mV, but the rest of the information on the plot is in V.
Raphael Brugger el 12 de Mayo de 2024
Oh. Thank you!

Iniciar sesión para comentar.

### Categorías

Más información sobre Simulation, Tuning, and Visualization en Help Center y File Exchange.

R2024a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by