how to plot the fundamental harmonic wave from given Data
12 次查看(过去 30 天)
显示 更早的评论
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1676136/image.png)
i used the same code as in https://de.mathworks.com/matlabcentral/answers/2049232-plot-the-fundamental-harmonic-wave,
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 个评论
采纳的回答
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 个评论
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
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');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1676151/image.png)
I hope this helps.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spectral Measurements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!