Difficulty in Calculating Signal Frequency with an Offset
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
I will also add the program and 2 example signals (one with and one without offset).
Thank you for your time!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
I will also add the program and 2 example signals (one with and one without offset).
Thank you for your time! 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
I will also add the program and 2 example signals (one with and one without offset).
Thank you for your time! fft, frequency, calculation, signal, signal processing, csv MATLAB Answers — New Questions