how to plot the fundamental harmonic wave from given Data
Mostrar comentarios más antiguos
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 comentario
akshatsood
el 22 de Abr. de 2024
Could you please elaborate on what "does not work" with the attached code.
Respuesta aceptada
Más respuestas (1)
akshatsood
el 22 de Abr. de 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 comentario
Abdullah
el 23 de Abr. de 2024
Categorías
Más información sobre Fourier Analysis and Filtering en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
