├── README.md ├── Conv_Wavelength.m ├── Error_Plot.m ├── LICENSE ├── TheRawData.m ├── Spectrum_Plot.m ├── Cross_Corr_Ranked.m ├── Main.m └── Shape_Determination.m /README.md: -------------------------------------------------------------------------------- 1 | # Spectral-Profile-Tracking 2 | MATLAB scripts used to track spectral shifts in multiplexed fiber Bragg gratings 3 | -------------------------------------------------------------------------------- /Conv_Wavelength.m: -------------------------------------------------------------------------------- 1 | function [ spectrum ] = Conv_Wavelength( x,y ) 2 | % This function converts raw data from the Micron Optics SM-130 interogator 3 | % to an array of reflectivities at given wavelengths 4 | 5 | %This part maps out the Micron Optics Range 1510nm to 1590nm 6 | s=size(x,1); 7 | dnm=(1590-1510)/s; 8 | 9 | %This part assigns the wavelength-reflectivities relationship 10 | % Column 1 is wavelength 11 | % Column 2 is the light intensity at that wavelength(AU) 12 | spectrum(:,1)=1510+x*dnm; 13 | spectrum(:,2)=y; 14 | 15 | end 16 | 17 | -------------------------------------------------------------------------------- /Error_Plot.m: -------------------------------------------------------------------------------- 1 | function [ ] = Error_Plot( Actual,Measured ) 2 | %% Pre-plotting Preparation 3 | %This is setting arbitrary time steps 4 | s=size(Actual,1); 5 | time=transpose(0:1:s-1); 6 | 7 | %% Plotting 8 | h=figure('units','normalized','outerposition',[0 0 1 1]); 9 | hold on 10 | plot(time,Actual(:,1),'Color','b','LineWidth',2.5) 11 | plot(time,Actual(:,2),'Color','r','LineWidth',2.5) 12 | plot(time,Measured(:,1),'--k','LineWidth',2.5) 13 | plot(time,Measured(:,2),'-.k','LineWidth',2.5) 14 | 15 | %% Format Plot 16 | xlabel('Time (AU)','FontSize',30) 17 | ylabel('Spectral Location (nm)','FontSize',30) 18 | legend('Actual FBG 1 Location','Actual FBG 2 Location','Measured FBG 1 Location','Measured FBG 2 Location','Location','NorthWest') 19 | set(gca,'FontSize',30) 20 | set(h,'PaperSize',[18 15]) 21 | set(h,'PaperPosition',[.25 .45 17.5 11.5]) 22 | 23 | %% Output File Format 24 | strpdf=['.\','Error_Plot.pdf']; 25 | streps=['.\','Error_Plot.eps']; 26 | strfig=['Error_Plot.fig']; 27 | 28 | %% Enable this section to save generated figure 29 | %print(h,strpdf,'-dpdf') 30 | %print(h,streps,'-depsc') 31 | %savefig(h,strfig) 32 | 33 | end 34 | 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 wjstewart 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /TheRawData.m: -------------------------------------------------------------------------------- 1 | 2 | i=0; 3 | %% Raw Data 4 | i=i+1; 5 | disp(['Spec ',num2str(i)]) 6 | tdfread('sample_1.txt'); 7 | s01=Conv_Wavelength(x,y); 8 | s(:,1)=s01(:,1); 9 | s(:,2)=s01(:,2); 10 | 11 | i=i+1; 12 | disp(['Spec ',num2str(i)]) 13 | tdfread('sample_2.txt'); 14 | s02=Conv_Wavelength(x,y); 15 | s(:,3)=s02(:,1); 16 | s(:,4)=s02(:,2); 17 | 18 | i=i+1; 19 | disp(['Spec ',num2str(i)]) 20 | tdfread('sample_3.txt'); 21 | s03=Conv_Wavelength(x,y); 22 | s(:,5)=s03(:,1); 23 | s(:,6)=s03(:,2); 24 | 25 | i=i+1; 26 | disp(['Spec ',num2str(i)]) 27 | tdfread('sample_4.txt'); 28 | s04=Conv_Wavelength(x,y); 29 | s(:,7)=s04(:,1); 30 | s(:,8)=s04(:,2); 31 | 32 | i=i+1; 33 | disp(['Spec ',num2str(i)]) 34 | tdfread('sample_5.txt'); 35 | s05=Conv_Wavelength(x,y); 36 | s(:,9)=s05(:,1); 37 | s(:,10)=s05(:,2); 38 | 39 | i=i+1; 40 | disp(['Spec ',num2str(i)]) 41 | tdfread('sample_6.txt'); 42 | s06=Conv_Wavelength(x,y); 43 | s(:,11)=s06(:,1); 44 | s(:,12)=s06(:,2); 45 | 46 | i=i+1; 47 | disp(['Spec ',num2str(i)]) 48 | tdfread('sample_7.txt'); 49 | s07=Conv_Wavelength(x,y); 50 | s(:,13)=s07(:,1); 51 | s(:,14)=s07(:,2); 52 | 53 | i=i+1; 54 | disp(['Spec ',num2str(i)]) 55 | tdfread('sample_8.txt'); 56 | s08=Conv_Wavelength(x,y); 57 | s(:,15)=s08(:,1); 58 | s(:,16)=s08(:,2); 59 | 60 | 61 | 62 | %% Clean up 63 | clear x y s01 s02 s03 s04 s05 s06 s07 s08 i 64 | clc -------------------------------------------------------------------------------- /Spectrum_Plot.m: -------------------------------------------------------------------------------- 1 | function [ Out ] = Spectrum_Plot( f,g,Correlation_Results,fig,lambda ) 2 | 3 | %% Figure Setup and Measured Spectrum Plotting 4 | h=figure('units','normalized','outerposition',[0 0 1 1]); % Generates the figure window and size 5 | plot(lambda,f,'Color','b','LineWidth',2.5) % Plots the measured spectrum 6 | xlim([1561.5 1564.5]); % Plot window 7 | ylim([-50 500]); % Plot window 8 | xlabel('Wavelength (nm)') % x axis label 9 | ylabel('Intensity (AU)') % y axis label 10 | str=['Measurement ',num2str(fig)]; % Generating figure title 11 | title(str) % Figure title 12 | set(gca,'FontSize',18) % Axis marks font size 13 | set(findall(gcf,'type','text'),'FontSize',18) % Label font size 14 | frmt=['k','r','g','m','c','y']; % Color vector for use later 15 | hold on 16 | 17 | %% Plotting Reference Spectra 18 | Out=zeros(size(g,2),1); 19 | for i=1:1:size(g,2) %Cycling through each cross-correlated FBG vector. 20 | tshape=lambda(size(lambda,1)-Correlation_Results(2,i)+1:size(lambda,1)-Correlation_Results(2,i)+size(g,1)); 21 | plot(tshape,g(:,i),'--','Color',frmt(i),'LineWidth',2.5) 22 | Out(i)=tshape(1); 23 | end 24 | legend('Full Spectrum','FBG Profile') 25 | 26 | %% Output File Format 27 | strpdf=['.\','Measurement_',num2str(fig),'.pdf']; 28 | streps=['.\','Measurement_',num2str(fig),'.eps']; 29 | strfig=['Measurement_',num2str(fig),'.fig']; 30 | 31 | %% Enable this section to save the generated figures 32 | %print(h,strpdf,'-dpdf') 33 | %print(h,streps,'-depsc') 34 | %savefig(h,strfig) 35 | 36 | end 37 | 38 | -------------------------------------------------------------------------------- /Cross_Corr_Ranked.m: -------------------------------------------------------------------------------- 1 | function [ Out,Correlation ] = Cross_Corr_Ranked( f,g ) 2 | %Cross_Corr This function cross correlates the given vectors 3 | f=transpose(f); 4 | 5 | %% Pre-allocating for speed 6 | L=size(g,2); %Number of FBGs 7 | L2=size(f,2)*2-1; %Size of Correlation Vector 8 | Out=zeros(2,L); %Output Pre-Allocating 9 | Correlation=zeros(L2,L); %Correlation Vector Pre-Allocating 10 | ranks=zeros(1,L); %Shape Ranking Pre-Allocating 11 | gnew=g; %Temporary variable for reference shapes 12 | fnew=f; %Temporary variable for measured spectrums 13 | 14 | %% Looping through each FBG 15 | for i=1:1:L 16 | %Finding the Biggest Area to rank the FBGs 17 | for j=1:1:L 18 | ranks(j)=trapz(gnew(:,j)); %MATLAB function for numerical integration 19 | end 20 | [~,location]=max(ranks); %Figuring out which FBG has biggest area. 21 | 22 | %Cross Correlating 23 | Correlation(:,location)=xcorr(gnew(:,location),fnew); %Generates correlation vector 24 | [max_value,index]=max(Correlation(:,location)); %Finds the maximum of the correlation vector 25 | % max_value=the value of the maximum point in the correlation vector 26 | % index=the location of the maximum value in the correlation vector 27 | 28 | %Recording values to output from the function 29 | Out(1,location)=max_value; 30 | Out(2,location)=index; 31 | 32 | %Resetting for the next run 33 | count=1; 34 | 35 | %This loop subtracts the cross-correlated FBG reference spectrum from 36 | %the from the measured spectrum, this is to prevent the subsequent 37 | %(smaller) FBG from being found "inside the larger shape of the current 38 | %FBG. 39 | for k=size(f,2)-1-index:1:size(f,2)-1-index+size(g,1)-1 40 | fnew(k)=fnew(k)-(gnew(count,location)); 41 | count=count+1; 42 | end 43 | 44 | %This removes the current FBG from the lineup of FBGs, so that it will 45 | %not be cross-correlated a second time. 46 | gnew(:,location)=0; 47 | 48 | end 49 | 50 | end 51 | 52 | -------------------------------------------------------------------------------- /Main.m: -------------------------------------------------------------------------------- 1 | clear all 2 | close all 3 | clc 4 | 5 | %% FBG Shapes 6 | %This is where your FBG data is put in. 7 | Shape_Determination 8 | 9 | %% Cross-Correlation 10 | count=0; loc=zeros(size(s,2)/2,size(FBG,2)); 11 | for i=2:2:size(s,2) 12 | %Iterating a Counting Variable 13 | count=count+1; 14 | 15 | %Cross Correlation Analysis 16 | [Correlation_Results,Correlation]=Cross_Corr_Ranked(s(:,i),FBG); 17 | 18 | %Plotting Individual Spetra 19 | [loc(count,:)]=Spectrum_Plot(s(:,i),FBG,Correlation_Results,count,s(:,i-1)); 20 | 21 | %Calculating Each FBG Spectral Location 22 | loc(count,:)=loc(count,:)+halfwidth; 23 | end 24 | 25 | %% Data Output 26 | count=0; output=zeros(size(s,2)/2,size(FBG,2)*2); 27 | for i=1:2:size(FBG,2)*2 28 | %Iterating a Counting Variable 29 | count=count+1; 30 | 31 | %Spacing Out the FBG Locations 32 | output(:,i)=loc(:,count); 33 | 34 | %Calculating the FBG Shift amounts 35 | for j=1:1:size(s,2)/2 36 | 37 | if j==1 %This Sets the First Shift to be Zero. 38 | output(j,i+1)=0; 39 | else %Each Shift is the Current Location Minus the Previous. 40 | output(j,i+1)=output(j,i)-output(j-1,i); 41 | end 42 | end 43 | end 44 | 45 | %Enable to write the Data to a .csv file. 46 | %csvwrite('Data.csv',output); 47 | %Format is of the form: 48 | % Column 1:FBG 1 location in spectrum 49 | % Column 2:delta_lambda between current location and previous location 50 | 51 | %% Actual Strain Calculations (Varies for experiment conducted) 52 | %Measured change in length of specimen: 53 | delL=[85.13-84.14;85.81-84.14;86.20-84.14;86.45-84.14;87.28-84.14;87.84-84.14;88.25-84.14;89.19-84.14]; 54 | %Calculated strain from delL above: 55 | Normalized=(delL+84.14)/84.14-1; 56 | %From calibration done of system at time of experiment: 57 | p=1000*[-0.169161572771762 0.040412766108891 1.561618980178803]; 58 | Actual_Strain(:,1)=p(1)*Normalized.^2+p(2)*Normalized+p(3); 59 | Actual_Strain(:,2)=loc(1,2); 60 | 61 | %% Trendline 62 | %This is the results from the Cross-Correlation: 63 | Measured_Strain(:,1)=loc(:,1); 64 | Measured_Strain(:,2)=loc(:,2); 65 | %Calculating the Root-Mean_Square Error for the Cross-Correlation: 66 | RMSError=zeros(1,size(FBG,2)); 67 | for k=1:1:size(FBG,2) 68 | diff=Actual_Strain-Measured_Strain; 69 | avg=mean(diff(:,k)); 70 | deviation=(diff(:,k)-avg).^2/size(diff,1); 71 | RMSError(k)=sqrt(sum(deviation)); 72 | end 73 | 74 | %% Error Plotting 75 | close all 76 | Error_Plot(Actual_Strain,Measured_Strain) 77 | 78 | -------------------------------------------------------------------------------- /Shape_Determination.m: -------------------------------------------------------------------------------- 1 | %% Import First Measured Spectrum 2 | clear all 3 | close all 4 | clc 5 | 6 | tdfread('sample_1.txt'); 7 | s01=Conv_Wavelength(x,y); 8 | s(:,1)=s01(:,1); 9 | s(:,2)=s01(:,2); 10 | 11 | %% Determining FBG Shapes 12 | %Preparing to plot the spectrum 13 | c(:,1)=s(:,1); 14 | c(:,2)=s(:,2); 15 | c(:,3)=1:1:size(c,1); 16 | 17 | %These outline the FBG locations 18 | st1=13548; %Start of FBG 1 19 | ed1=13742; %End of FBG 1 20 | st2=13742; %Start of FBG 2 21 | ed2=13978; %End of FBG 2 22 | %st3=num; %Enable these to add FBGs 23 | %ed3=num; 24 | 25 | %%Enable this section to modify st1,ed1,st2,ed2,... 26 | % plot(c(:,3),c(:,2)) 27 | % keyboard 28 | 29 | %Cutting out the FBG shapes defined by the above st# and ed# locations 30 | count=0; 31 | for i=st1:1:ed1 %This iterates the span of FBG1 32 | count=count+1; 33 | FBG1(count,1)=c(i,1); 34 | FBG1(count,2)=c(i,2); 35 | end 36 | count=0; 37 | for j=st2:1:ed2 %This iterates the span of FBG2 38 | count=count+1; 39 | FBG2(count,1)=c(j,1); 40 | FBG2(count,2)=c(j,2); 41 | end 42 | f1=size(FBG1,1); %Assigning Variables for the size of the two FBGs 43 | f2=size(FBG2,1); %Assigning Variables for the size of the two FBGs 44 | 45 | %% Enable this section to verify FBG Shape choices (st1,ed1,st2,ed2,...) 46 | % figure('units','normalized','outerposition',[0 0 1 1]); 47 | % hold on 48 | % plot(FBG1(:,1),FBG1(:,2),'Color','b','LineWidth',2.5) 49 | % plot(FBG2(:,1),FBG2(:,2),'Color','g','LineWidth',2.5) 50 | % xlabel('Wavelength (nm)') 51 | % ylabel('Intensity (AU)') 52 | % set(gca,'FontSize',18) 53 | % keyboard 54 | 55 | %% Padding with "zeros" such that FBG1 and FBG2 are the same length 56 | %The loop finds the average reflectivity after the last FBG in the spectrum 57 | sum=0; 58 | for l=ed2+1:1:size(s,1) 59 | sum=sum+c(l,2); 60 | end 61 | avg=sum/(size(c,1)-(ed2+1)); 62 | 63 | %The shorter FBG is padded with the avg found in the loop above 64 | for m=1:1:f2 65 | if m<=f1 66 | FBG1_new(m,2)=FBG1(m,2); 67 | FBG2_new(m,2)=FBG2(m,2); 68 | end 69 | if m>f1 70 | FBG1_new(m,2)=avg; 71 | FBG2_new(m,2)=FBG2(m,2); 72 | end 73 | end 74 | 75 | %This part makes sure that the reflectivities are >=0 76 | FBG(:,1)=FBG1_new(:,2)-avg; 77 | FBG(:,2)=FBG2_new(:,2)-avg; 78 | 79 | %% Width Analysis 80 | %This section of code is used to find the midpoint of the FBG shape. 81 | %This is used later to track the change in FBG location needed to determine 82 | %the strain. 83 | for j=1:1:size(FBG,2) 84 | [~,index]=max(FBG(:,j)); 85 | ind(:,j)=index; 86 | end 87 | halfwidth(:,1)=c(st1+ind(1),1)-c(st1,1); 88 | halfwidth(:,2)=c(st2+ind(2),1)-c(st2,1); 89 | 90 | %% Enable to verify modifications to FBG shape arrays has been successful. 91 | % figure 92 | % hold on 93 | % plot(1:1:f2,FBG1_new(:,2)-avg,'Color','b') 94 | % plot(1:1:f2,FBG2_new(:,2)-avg,'Color','g') 95 | % xlabel('Wavelength (nm)') 96 | % ylabel('Intensity (AU)') 97 | % keyboard 98 | 99 | %% This part brings in all the rest of the data 100 | TheRawData 101 | 102 | %This loop reformats the new measured spectrum data using the avg 103 | %calculated above. 104 | for n=2:2:size(s,2) 105 | s(:,n)=s(:,n)-avg; 106 | end 107 | 108 | 109 | %% Clearing Variables 110 | clear tar01 tar02 count i j c ans avg1 avg2 l k x y j maxim index ind 111 | clear fin sum m f1 f2 FBG1_new FBG2_new FBG1 FBG2 h s01 n st1 st2 ed1 ed2 112 | 113 | --------------------------------------------------------------------------------