Explaining what MatLab is doing
2 views (last 30 days)
Show older comments
Hi Everyone,
In a Signals class and this is part of an already turned in Lab. I'm trying to better comprehend what MatLab is doing behind the scenes here and how the parameters/arguments are mapping inside of the programmatic language thorugh each step here into the array (or is it matrix) A_k.
This is Fourier analysis, and the different steps building along the way to get to A_k are confusing me a bit. Any detail would be greatly appreciated.
Below code is pasted, but also attached as .m file.
Thank you, friends...
%Part 1 Working within the Time Domain
% Import your waveform and time data
myID = 18042743; %Not my real ID
[Y_t, T_s ] = BuildingTraceData(myUFID);%BuildingTraceData was a p file
% Question 1
figure
plot(T_s, Y_t, LineWidth=1)
title('Time Domain of Vibrations')
ylabel('Amplitude')
xlabel('Time')
% Question 2
% The signal is periodic. The signal shows repetition with peaks at
% x=0.2959 y=34.5699 and x=0.3959 y=34.5699
x1=0.2959;
x2=0.3959;
% Question 3
T_0 = x2-x1;
T_0 = 0.1; % Fundamental Period (must fill in answer here)
%Question 4
f_0 = 1/T_0;
%Part 2 Fourier Series (Analysis)
% Question 5
% The below code is given as a starting point.
K = 10;
% Find Index point within Ts for the first T_o
[~,L_T_o]=min(abs(T_0-T_s)); %
%Pre-Allocation of space for Matrices (speeds up Matlab)
Kernel_R = zeros(K,L_T_o);
Kernel_I = zeros(K,L_T_o);
size(L_T_o);
L_T_o;
size(Kernel_I);
index=1:L_T_o;
% Calculation of coefficient matrix
% trapz was used as a way to approximate the integral
% help trapz may be useful to type into your command window
1/T_0;
f_0;
n=1;
for k = -K:K
Kernel_R(n,:) = Y_t(index).*cos(2*pi*k*T_s(index)*f_0);
Kernel_I(n,:) = Y_t(index).*sin(2*pi*k*T_s(index)*f_0);
A_R_k(n) = 1*f_0*trapz(T_s(index), Kernel_R(n,:)); % Real
A_I_k(n) = 1*f_0*trapz(T_s(index), Kernel_I(n,:)); % Imaginary
A_k(n)= A_R_k(n)-j*A_I_k(n); % j because EE
n=1+n ;
end
A_k
4 Comments
David Goodmanson
on 21 Oct 2024
Hello Christina.
AI can help, although you tend to end up with some pretty generic information. Good if it works. I have some doubts about the line [~,L_T_o]=min(abs(T_0-T_s)), which determines the maximum value of the index array 'index'. But since the data is not available from BuildingTraceData, it's not possible to run the code. It would help if you could attach the data, either as a mat file or (if the size is not large) a text file.
Accepted Answer
Ayush
on 22 Oct 2024
Your given MATLAB code performs analysis on a waveform signal in the time domain and computes its Fourier series coefficients.
I’ll try to provide a breakdown of the code, dividing it into sections:
- Loading Data: First, the code imports the waveform and time data using a function called "BuildingTraceData", which takes a user ID as input. It stores the signal in Y_t and the time data in T_s.
myID = 18042743; % User ID
[Y_t, T_s] = BuildingTraceData(myUFID); % Import data
2. Plotting the Signal: Next, it creates a plot to visualize the waveform. The x-axis represents time, while the y-axis shows the amplitude of the vibrations. The plot is titled "Time Domain of Vibrations."
figure
plot(T_s, Y_t, 'LineWidth', 1)
title('Time Domain of Vibrations')
ylabel('Amplitude')
xlabel('Time')
3. Identifying Periodicity: The code identifies specific time points (x1 and x2) where peaks occur in the signal. This helps in determining the signal's periodic nature.
x1 = 0.2959;
x2 = 0.3959;
4. Calculating the Fundamental Period: The fundamental period (T_0) is calculated by finding the difference between x2 and x1. However, it seems there's a manual overwrite that sets T_0 to 0.1.
T_0 = x2 - x1;
T_0 = 0.1;
5. Finding the Fundamental Frequency: Using the period, it calculates the fundamental frequency (f_0), which indicates how often the signal repeats.
f_0 = 1/T_0;
Part 2: Fourier Series Analysis
- Setting Up for Fourier Analysis:The code defines how many harmonics to consider (K = 10) and finds the index in the time data that corresponds to the closest value to the fundamental period.
K = 10;
[~, L_T_o] = min(abs(T_0 - T_s));
2. Pre-allocating Space for Coefficients:It prepares matrices for storing the real and imaginary parts of the Fourier coefficients. This helps improve efficiency during calculations.
Kernel_R = zeros(K, L_T_o);
Kernel_I = zeros(K, L_T_o);
3. Calculating Fourier Coefficients:The heart of the code is a loop that goes through each harmonic from -K to K. For each harmonic, it calculates:
for k = -K:K
Kernel_R(n,:) = Y_t(index).*cos(2*pi*k*T_s(index)*f_0);
Kernel_I(n,:) = Y_t(index).*sin(2*pi*k*T_s(index)*f_0);
A_R_k(n) = 1*f_0*trapz(T_s(index), Kernel_R(n,:));
A_I_k(n) = 1*f_0*trapz(T_s(index), Kernel_I(n,:));
A_k(n) = A_R_k(n) - j*A_I_k(n);
n = 1 + n;
end
- The real part using a cosine function.
- The imaginary part using a sine function.
- It then uses numerical integration ("trapz") to compute the coefficients, which are stored in a complex array.
Final Output:
The computed Fourier coefficients are stored in A_k, ready for further analysis or use.
If you face any further issus, feel free to comment.
Hope it helps!
0 Comments
More Answers (0)
See Also
Categories
Find more on Spectral Estimation 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!