how can I convert linear figure's axis to logarithmic?

16 Ansichten (letzte 30 Tage)
Adnan
Adnan am 26 Mär. 2025
Kommentiert: Adnan am 8 Apr. 2025
Hi,
I have following Figure#1, I want to convert only y-axis to logaritmic scale. I tried several ways but each time outcome is not normal. Can you please tell me how to achive y-axis of Figure#2. Thanks for you help.
% this my figure script.
figure;
imagesc(t, fliplr(f), ST_normalized)
previously I used the follwing command, it did not work. I want to have the image of Figure#2 in y-axis.
for instance, "10" in Figure#1 shoud be "10^1" in a new scaled figure.
set(gca, 'YScale', 'log')

Akzeptierte Antwort

Star Strider
Star Strider am 26 Mär. 2025
I am not certain what the problem is, however you can change the ruler properties with the Exponent property.
N = 100;
x = linspace(0, 1, N);
y = linspace(1, 50, N);
figure
plot(x, y)
grid
figure
plot(x, y)
grid
Ax = gca;
Ax.YScale = 'log';
Ax.YAxis.Exponent = 1;
.
  15 Kommentare
Star Strider
Star Strider am 8 Apr. 2025
MATLAB Online is clkearly having problems with your code, however is throwing no errors or warnings.
A whos call just before the second plot returns:
Name Size Bytes Class Attributes
EQ 18583x1 148664 double
KUT_090 18583x2 297328 double
ST 9292x18583 2762771776 double complex
ST_normalized 9292x18583 1381385888 double
dt 1x1 8 double
f 1x9292 74336 double
max_amplitude 1x18583 148664 double
maxfreq 1x1 8 double
minfreq 1x1 8 double
samplingrate 1x1 8 double
t 1x18583 148664 double
time 18583x1 148664 double
uz 1x3 498 cell
zipf 1x30 60 char
zipfn 1x87 174 char
Ths ‘ST’ and ‘ST_normalized’ arrays together require 4,144,157,664 bytes. That could be a problem.
I am running this script:
% % % Adnan_2025_04_08.m
clear
close all
zipfn = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1831478/MATLAB_FILES.zip';
zipf = websave('MATLAB_FILES.zip',zipfn);
uz = unzip(zipf)
% % fprintf(1,['\n' repmat('=',1,72) '\n'])
% %
% % type(uz{2})
% %
% % fprintf(1,['\n' repmat('=',1,72) '\n'])
% %
% % type(uz{3})
% %
% % fprintf(1,['\n' repmat('=',1,72) '\n'])
% return
tic
%% Main RUN
%%
% clear all;
% clc
%%
% load KUT_090.txt
load(uz{1})
EQ=KUT_090(:,2);
time=KUT_090(:,1);
dt = mean(diff(time));
%%
minfreq=0;
maxfreq=length(EQ) ;
samplingrate=dt ;
[ST,t,f]=s_trans_st(EQ,minfreq,maxfreq,samplingrate);
max_amplitude = max(abs(ST), [], 1);
ST_normalized = abs(ST)./ max_amplitude;
ST_normalized=flipud(ST_normalized);
%%
figure;
imagesc(t, fliplr(f), ST_normalized)
ax = gca;
ax.YDir = 'normal';
ylabel('Frequency, Hz')
xlabel('Time (s)')
colorbar;
colormap(jet(256));
%%
%%
%%
figure;
surf(t, fliplr(f), ST_normalized, EdgeColor='none')
ax = gca;
ax.YDir = 'normal';
ylabel('Frequency, Hz')
xlabel('Time (s)')
colorbar;
colormap(jet(256));
view(0,90)
axis('tight')
ax.YScale = 'log';
ax.YAxis.Exponent = 1;
yline(0.305, 'c--', 'LineWidth', 2); % sys_ini, FF1
yline(0.286, 'black--', 'LineWidth', 2); % sys_inc_m, FF1
xline(70.03, 'r--', 'LineWidth', 2); %sys_ini, FF1
xline(172.94, 'black--', 'LineWidth', 2) ; %sys_inc_m ,,, FF1
toc
% ========================================================================
function [st,t,f] = s_trans_st(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate)
verbose = true;
removeedge= false;
analytic_signal= isreal(timeseries);
factor = 3;
% END of DEFAULT PARAMETERS
% START OF INPUT VARIABLE CHECK
% First: make sure it is a valid time_series. If not, return the help message
if verbose, disp(newline), end % i like a line left blank
if nargin==0
if verbose, disp('No parameters inputted.'),end
st_help
t=0; st=-1; f=0;
return
end
% Make sure it is a 1-dimensional array
assert(numel(timeseries)>1,'Please enter a *vector* of data, not a scalar')
assert(isvector(timeseries),'Please enter a *vector* of data, not matrix')
% Ensure is column vector
if ~iscolumn(timeseries), timeseries=timeseries.'; end
% use defaults for input variables not provided
switch nargin
case 1
minfreq = 0;
maxfreq = fix(length(timeseries)/2);
samplingrate=1;
freqsamplingrate=1;
case 2
maxfreq = fix(length(timeseries)/2);
samplingrate=1;
freqsamplingrate=1;
case 3
samplingrate=1;
freqsamplingrate=1;
%[minfreq,maxfreq,samplingrate,freqsamplingrate] = check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
case 4
freqsamplingrate=1;
%[minfreq,maxfreq,samplingrate,freqsamplingrate] = check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
case 5
%[minfreq,maxfreq,samplingrate,freqsamplingrate] = check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
otherwise
if verbose, disp('Error in input arguments: using defaults'), end
minfreq = 0;
maxfreq = fix(length(timeseries)/2);
samplingrate=1;
freqsamplingrate=1;
end
[minfreq,maxfreq,samplingrate,freqsamplingrate]= check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries);
if verbose
fprintf('Minfreq = %d a/n',minfreq)
fprintf('Maxfreq = %d a/n',maxfreq)
fprintf('Sampling Rate (time domain) = %d a/n',samplingrate)
fprintf('Sampling Rate (freq. domain) = %d a/n',freqsamplingrate)
fprintf('The length of the timeseries is %d points\n',length(timeseries))
end %END OF INPUT VARIABLE CHECK
% calculate the sampled time and frequency values from the two sampling rates
t = (0:length(timeseries)-1)*samplingrate;
spe_nelements =ceil((maxfreq - minfreq+1)/freqsamplingrate) ;
f = (minfreq + (0:spe_nelements-1)*freqsamplingrate)/(samplingrate*length(timeseries));
if verbose, fprintf('The number of frequency voices is %d\n',spe_nelements),end
% The actual S Transform function is here:
st = strans(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,removeedge,analytic_signal,factor);
% if no output, then plot amplitude spectrum
if nargout==0
if verbose, disp('Plotting pseudocolor image'),end
imagesc(t,f,abs(st))
colormap('jet')
colorbar
end
end
function st = strans(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,removeedge,analytic_signal,factor)
n=length(timeseries);
original = timeseries;
if removeedge
if verbose, disp('Removing trend with quadratic polynomial fit'),end
ind = (0:n-1).';
r = polyfit(ind,timeseries,2);
fit = polyval(r,ind) ;
timeseries = timeseries - fit;
if verbose, disp('Removing edges with 5% hanning taper'),end
sh_len = floor(length(timeseries)/10);
wn = hanning(sh_len);
if(sh_len==0)
sh_len=length(timeseries);
wn = 1&[1:sh_len];
end
% make sure wn is a column vector, because timeseries is
if size(wn,2) > size(wn,1)
wn=wn';
end
timeseries(1:floor(sh_len/2),1) = timeseries(1:floor(sh_len/2),1).*wn(1:floor(sh_len/2),1);
timeseries(length(timeseries)-floor(sh_len/2):n,1) = timeseries(length(timeseries)-floor(sh_len/2):n,1).*wn(sh_len-floor(sh_len/2):sh_len,1);
end
% If vector is real, do the analytic signal
if analytic_signal
if verbose, disp('Calculating analytic signal (using Hilbert transform)'),end
% this version of the hilbert transform is different than hilbert.m
% This is correct!
ts_spe = fft(real(timeseries));
h = [1; 2*ones(fix((n-1)/2),1); ones(1-rem(n,2),1); zeros(fix((n-1)/2),1)];
ts_spe(:) = ts_spe.*h(:);
timeseries = ifft(ts_spe);
end
% Compute FFT's
tic;vector_fft=fft(timeseries);tim_est=toc;
vector_fft=[vector_fft,vector_fft];
tim_est = tim_est*ceil((maxfreq - minfreq+1)/freqsamplingrate) ;
if verbose, fprintf('Estimated time is %f',tim_est),end
% Preallocate the STOutput matrix
st=zeros(ceil((maxfreq - minfreq+1)/freqsamplingrate),n);
% Compute the mean
% Compute S-transform value for 1 ... ceil(n/2+1)-1 frequency points
if verbose, disp('Calculating S transform...'),end
if minfreq == 0
st(1,:) = mean(timeseries)*(1&[1:1:n]);
else
st(1,:)=ifft(vector_fft(minfreq+1:minfreq+n).*g_window(n,minfreq,factor));
end
%the actual calculation of the ST
% Start loop to increment the frequency point
for banana=freqsamplingrate:freqsamplingrate:(maxfreq-minfreq)
st(banana/freqsamplingrate+1,:)=ifft(vector_fft(minfreq+banana+1:minfreq+banana+n).*g_window(n,minfreq+banana,factor));
end % a fruit loop! aaaaa ha ha ha ha ha ha ha ha ha ha
% End loop to increment the frequency point
if verbose, disp('Finished Calculation'),end
end %%% end strans function
%------------------------------------------------------------------------
function gauss=g_window(len,freq,factor)
vector(1,:)=(0:len-1);
vector(2,:)=(-len:-1);
vector=vector.^2;
vector=vector*(-factor*2*pi^2/freq^2);
% Compute the Gaussion window
gauss=sum(exp(vector));
end
function [minfreq,maxfreq,samplingrate,freqsamplingrate]=check_input(minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,timeseries)
% this checks numbers, and replaces them with defaults if invalid
% if the parameters are passed as an array, put them into the appropriate variables
s = size(minfreq);
l = max(s);
if l > 1
if verbose, disp('Array of inputs accepted.'), end
temp=minfreq;
minfreq = temp(1);
if l > 1, maxfreq = temp(2); end
if l > 2, samplingrate = temp(3); end
if l > 3, freqsamplingrate = temp(4); end
if l > 4
if verbose, disp('Ignoring extra input parameters.'),end
end
end
if minfreq<0 || minfreq>fix(length(timeseries)/2)
minfreq = 0;
if verbose, disp('Minfreq < 0 or > Nyquist. Setting minfreq = 0.'),end
end
if maxfreq>length(timeseries)/2 || maxfreq<0
maxfreq = fix(length(timeseries)/2);
if verbose, fprintf('Maxfreq < 0 or > Nyquist. Setting maxfreq = %d a/n',maxfreq),end
end
if minfreq>maxfreq
temporary = minfreq;
minfreq = maxfreq;
maxfreq = temporary;
if verbose, disp('Swapping maxfreq <=> minfreq.'),end
end
if samplingrate<0
samplingrate = abs(samplingrate);
if verbose, disp('Samplingrate <0. Setting samplingrate to its absolute value.'),end
end
if freqsamplingrate<0 % check 'what if freqsamplingrate > maxfreq - minfreq' case
freqsamplingrate=abs(freqsamplingrate);
if verbose, disp('Frequency Samplingrate negative, taking absolute value'),end
end
end % check_input
function st_help
disp(' ')
disp('st() HELP COMMAND')
disp('st() returns - 1 or an error message if it fails')
disp('USAGE:: [localspectra,timevector,freqvector] = st(timeseries)')
disp('NOTE:: The function st() sets default parameters then calls the function strans()')
disp(' ')
disp('You can call strans() directly and pass the following parameters')
disp(' **** Warning! These inputs are not checked if strans() is called directly!! ****')
disp('USAGE:: localspectra = strans(timeseries,minfreq,maxfreq,samplingrate,freqsamplingrate,verbose,removeedge,analytic_signal,factor) ')
disp(' ')
disp('Default parameters (available in st.m)')
disp('VERBOSE - prints out informational messages throughout the function.')
disp('REMOVEEDGE - removes the edge with a 5% taper, and takes')
disp('FACTOR - the width factor of the localizing gaussian')
disp(' ie, a sinusoid of period 10 seconds has a ')
disp(' gaussian window of width factor*10 seconds.')
disp(' I usually use factor=1, but sometimes factor = 3')
disp(' to get better frequency resolution.')
disp(' ')
disp('Default input variables')
disp('MINFREQ - the lowest frequency in the ST result(Default=0)')
disp('MAXFREQ - the highest frequency in the ST result (Default=nyquist')
disp('SAMPLINGRATE - the time interval between successive data points (Default = 1)')
disp('FREQSAMPLINGRATE - the number of frequencies between samples in the ST results')
end % of st_help procedure
When I suppress the first plot by commenting it out, nothing plots and the code crashes. Setting the YScale value does not change that behaviour. The elapsed time (given by the tic and toc calls I added) is generally 20-22 seconds. Available memory could be a problem, however I would expect an error or warning in that instance. I am not certain what you are doing, so I cannot suggest any changes.
.
Adnan
Adnan am 8 Apr. 2025
Thanks for that. Interestingly, the whole code runs fine once I use the following figuring.
figure;
imagesc(t, fliplr(f), ST_normalized)
ax = gca;
ax.YDir = 'normal';
ylabel('Frequency, Hz')
xlabel('Time (s)')
colorbar;
colormap(jet(256));
Nonetheless, when I execute the following script, it freezes. I would like to know what is wrong with the following scripts. I think the only problem is with the following scripts. The follwing script appears to be correct, but something is causing the plotting to freeze. Is that possible we can solve this issue? Thanks again @Star Strider sir for your help.
figure;
surf(t, fliplr(f), ST_normalized, EdgeColor='none')
ax = gca;
ax.YDir = 'normal';
ylabel('Frequency, Hz')
xlabel('Time (s)')
colorbar;
colormap(jet(256));
view(0,90)
axis('tight')
ax.YScale = 'log';
ax.YAxis.Exponent = 1;

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Descriptive Statistics finden Sie in Help Center und File Exchange

Produkte


Version

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by