how to plot the fundamental harmonic wave from given Data

37 Ansichten (letzte 30 Tage)
Abdullah
Abdullah am 22 Apr. 2024
Kommentiert: Mathieu NOE am 13 Mai 2024
I have exported data to an Excel spreadsheet where the x-axis is represented by theta (in radians) and y-axis is the flux density distribution. How can I plot the fundamental harmonic wave from this wave data?
but it does not work. the mat file is attached.
%%%%%%%%%%%%% main code %%%%%%%%%%%%%%%%%
% 2nd order extraction (DFT)
order = 2;
% model fit : X = A*cos(order*theta) + B*sin(order*theta) + C
C = mean(y);
y = y-C;
n = numel(theta);
A = 2/n*trapz(y.*cos(order*theta));
B = 2/n*trapz(y.*sin(order*theta));
yfit = A*cos(order*theta) + B*sin(order*theta) + C;
% plot
figure(1),
plot(theta, y, 'b',theta, yfit, 'r')
legend('data','model fit');
  1 Kommentar
akshatsood
akshatsood am 22 Apr. 2024
Could you please elaborate on what "does not work" with the attached code.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Mathieu NOE
Mathieu NOE am 24 Apr. 2024
hello
here you have 4 periods of signal for theta range of 2pi , so order = 4
load('FluxDensity.mat')
%%%%%%%%%%%%% main code %%%%%%%%%%%%%%%%%
% 4th order extraction (DFT)
order = 4;
% model fit : X = A*cos(order*theta) + B*sin(order*theta) + C
C = mean(y);
y = y-C;
n = numel(theta);
A = 2/n*trapz(y.*cos(order*theta));
B = 2/n*trapz(y.*sin(order*theta));
yfit = A*cos(order*theta) + B*sin(order*theta) + C;
% plot
figure(1),
plot(theta, y, 'b',theta, yfit, 'r')
legend('data','model fit');
  3 Kommentare
Abdullah
Abdullah am 8 Mai 2024
Hello Mathieu,
could you please just check the new attached file" FluxDensity_new.mat" and see if you could plot the first harmonic? because the use the same code and change the order to 1 but did not work.
thank you in advance!
Mathieu NOE
Mathieu NOE am 13 Mai 2024
hello again
it didn't work because the code assumes theta is given in rads as it was in the previous case
your new file gives theta in degrees so you have to convert first in radians
load('FluxDensity_new.mat')
% y = Br_noLoad;
y = Br_WithLoad;
theta = theta*pi/180;
%%%%%%%%%%%%% main code %%%%%%%%%%%%%%%%%
% order extraction (DFT)
order = 1;
% model fit : X = A*cos(order*theta) + B*sin(order*theta) + C
C = mean(y);
y = y-C;
n = numel(theta);
A = 2/n*trapz(y.*cos(order*theta));
B = 2/n*trapz(y.*sin(order*theta));
yfit = A*cos(order*theta) + B*sin(order*theta) + C;
% plot
figure(1),
plot(theta, y, 'b',theta, yfit, 'r')
legend('data','model fit');
xlable('theta (rad)');

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

akshatsood
akshatsood am 22 Apr. 2024
I would like to share insights on alternative approach to plot the fundamental harmonic wave of your data in MATLAB leveraging the bandpass() and fft() function. This approach involves first identifying the fundamental frequency of your signal using the Fourier Transform (via fft()), and then filtering around that frequency with bandpass() to isolate the fundamental harmonic.
Step 1: Prepare the Signal for FFT
First, you need to convert theta to a time or space domain that makes sense for FFT, which requires evenly spaced samples. Assuming theta is evenly spaced, sampling frequency can be computed as follows
Fs = 1 / mean(diff(theta)); % Sampling frequency
Step 2: Use FFT to Find the Fundamental Frequency
L = length(y); % Length of the signal
Y = fft(y); % Compute the FFT
P2 = abs(Y/L); % Two-sided spectrum
P1 = P2(1:L/2+1); % Single-sided spectrum
P1(2:end-1) = 2*P1(2:end-1);
f = Fs*(0:(L/2))/L; % Frequency domain
% Find the fundamental frequency (ignoring DC component)
[~, I] = max(P1(2:end)); % Index of max peak
fundamentalFreq = f(I+1); % Fundamental frequency, offset by 1 due to ignoring DC
Step 3: Use bandpass() to Isolate the Fundamental Harmonic
Let us center this around fundamentalFreq and specify a small bandwidth. Make sure y is suitable for bandpass()
bw = fundamentalFreq * 0.1; % bandwidth: 10% of the fundamental frequency
filteredSignal = bandpass(y, [fundamentalFreq-bw, fundamentalFreq+bw], Fs);
Step 4: Plot the Original and Filtered Signals
figure;
plot(theta, y); % Original signal
hold on;
plot(theta, filteredSignal, 'LineWidth', 2); % Filtered signal showing fundamental harmonic
legend('Original Data', 'Fundamental Harmonic');
xlabel('\theta (radians)');
ylabel('Flux Density Distribution');
title('Fundamental Harmonic Isolation');
I hope this helps.
  1 Kommentar
Abdullah
Abdullah am 23 Apr. 2024
Hello akshatsood,
Thank you for your detailed explanaition.
i have expected that the amplitude of the fundamental harmonic should be near to the original, in this solution seams that it is very low which i did not expect, it is attenuating in the fourth cycle.
i really wondering that Matlab does not provide easy solution.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by