Effacer les filtres
Effacer les filtres

Difficulty in Calculating Signal Frequency with an Offset

12 vues (au cours des 30 derniers jours)
Raphael Brugger
Raphael Brugger le 11 Mai 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:
% Reading .csv
data = readmatrix('Sinus.csv');
% 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
Thank you for your time!

Réponse acceptée

Paul
Paul le 11 Mai 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.
% Reading .csv
data = readmatrix('SinusOff.csv');
% 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 commentaires
Paul
Paul le 11 Mai 2024
The mean is in mV, but the rest of the information on the plot is in V.
Raphael Brugger
Raphael Brugger le 12 Mai 2024
Oh. Thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Simulation, Tuning, and Visualization dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by