how to plot the fundamental harmonic wave from given Data
33 views (last 30 days)
Show older comments
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?
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 Comment
akshatsood
on 22 Apr 2024
Could you please elaborate on what "does not work" with the attached code.
Accepted Answer
Mathieu NOE
on 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 Comments
Mathieu NOE
on 13 May 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)');
More Answers (1)
akshatsood
on 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.
See Also
Categories
Find more on Spectral Measurements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!