Search code examples
matlabplotmatlab-figurenormalization

Normalization of a plot


I need to normalize a plot to a grayscale from 0 to 255. The data is in .txt format. This is the rough plotting that I have:

clf;
%call importdata() after changing current directory
B = importdata('sigpros.txt');

d  = 1000; %vertical spacing
Bs = B;    %copy of the original data

for i = 1 : size(Bs,2)
    %loop adding constant to each column
    Bs(:,i) = Bs(:,i) + (i-1) * d;
end    

%plot the modified matrix
plot(Bs);

The data consist of 349 rows and 4007 columns. Each column is a full A-scan data (a wave form). Each data has a vertical spacing and a full set of these plotted data makes a B-scan data (Waveform attained from displacement of a sensor). I am not sure if the code above is correct but the data should look something like: B-Scan data.

This can be attained with normalization of the matrix plot above to a grayscale 0 to 255. Currently, this is how my plot looks like: My plot. Please help me get the desired B-scan plot like above! Thanks!

UPDATES

This is the normalized b-scan data. However, the way it initially peaks is higher than the one in the above image. What could be the problem here?

Zero offset removal

clf;
%call importdata() after changing current directory
B = importdata('A_scan1.txt');
Bd = detrend(B,0); %remove the zero offset

d  = 1000; %vertical spacing
Bs = Bd;    %copy of the original data

for i = 1 : size(Bs,2)
    %loop adding constant to each column
    Bs(:,i) = Bs(:,i) + (i-1) * d;
end    

minV = min(Bs(:));
maxV = max(Bs(:));
Bs_scale = (Bs-minV)*255/(maxV-minV);

%plot the modified matrix
plot(Bs_scale, 'k');

However, it still doesn't start from 0.


Solution

  • This will set on offset so the starting point of the first line is at zero and scale all the data so that the Range from min to max value is 255 units.

    I commented out but included some lines that would alternative scale the data so that it starts at 0 & the peak is at 255. However, since you have some negative values then the total range is > 255.

    clf;
    %call importdata() after changing current directory
    B = importdata('sigpros.txt');
    
    %NOTE: I am only plotting the first 59 lines as the rest don't look as good
    Bd = detrend(B(:,1:59),0); %remove the zero offset
    
    d  = 1000; %vertical spacing
    Bs = Bd;    %copy of the original data
    
    %Get the initial zero offset from the first line
    initOffset = Bs(1,1);
    %% xxx Alternatively take the mean across all starting points xxx
    % initOffset = mean(Bs(1,:));
    
    for i = 1 : size(Bs,2)
        %loop adding constant to each column
        Bs(:,i) = Bs(:,i) - initOffset + (i-1) * d ; %subtract the offset from each
    end    
    
    minV = min(Bs(:));
    maxV = max(Bs(:));
    
    %This make the RANGE from min to make = 0-255 units.
    Bs_scale = (Bs)*255/(maxV-minV);
    %% xxxx If instead you want the peak to be at 255 xxxxx
    % Bs_scale = (Bs)*255/(maxV);
    
    %plot the modified matrix
    plot(Bs_scale, 'k');
    

    EDIT/Explanation:

    Here is what B looks like Raw.. It is basically a series of lines all on top of each other. After you detrend it removes most of the constant offset. However, since this signal isn't perfectly symmetrical these lines do not start exactly at zero ... they are closer than they were but are not perfect. Here is Bd after detrend Note that each line does not start at exactly zero.

    Next your for loop originally spaced each line apart by 1000 by added a multiple of d so it looked like this. Since theses lines do not start exactly at zero which is what you asked for I added the initial offset term. These basically takes the first point of the first line and subtracts that from every line also. Thus forcing it to begin at exactly zero.