Difficulty in Calculating Signal Frequency with an Offset
7 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Raphael Brugger
am 11 Mai 2024
Bearbeitet: Raphael Brugger
am 12 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!
0 Kommentare
Akzeptierte Antwort
Paul
am 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
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 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Simulation, Tuning, and Visualization finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!