how to plot the fundamental harmonic wave from given Data

조회 수: 40 (최근 30일)
Abdullah
Abdullah 2024년 4월 22일
댓글: Mathieu NOE 2024년 5월 13일
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
akshatsood
akshatsood 2024년 4월 22일
Could you please elaborate on what "does not work" with the attached code.

댓글을 달려면 로그인하십시오.

채택된 답변

Mathieu NOE
Mathieu NOE 2024년 4월 24일
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
Abdullah
Abdullah 2024년 5월 8일
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 2024년 5월 13일
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)');

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

akshatsood
akshatsood 2024년 4월 22일
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
Abdullah
Abdullah 2024년 4월 23일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Spectral Measurements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by