Clear Filters
Clear Filters

Difficulty in Calculating Signal Frequency with an Offset

7 views (last 30 days)
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!

Accepted Answer

Paul
Paul on 11 May 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 Comments
Paul
Paul on 11 May 2024
The mean is in mV, but the rest of the information on the plot is in V.

Sign in to comment.

More Answers (0)

Categories

Find more on Simulation, Tuning, and Visualization in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!