matlab calculating delta between plotted lines - matlab

I am producing this graph (reduced code version) :
k = 1000
r = [100 220 470 1*k 2200 4700 10*k 22*k 47*k 100*k 220*k 470*k 1000*k ]
unModNB = [0.72 0.746 0.801 0.92 1.16 1.69 2.78 4.6 6.45 9.1 11.2 12.4 13.2]
unModWB = [1.124 1.17 1.23 1.48 1.84 2.65 4.2 7.6 11.8 15.4 18.6 20.01 21.7]
ModNBdB = 20*log10( ModNB)
ModWBdB = 20*log10( ModWB)
semilogx( r, ModNBdB, r, ModWBdB )
grid
legend(Line 1 ', 'Line 2')
How am I able to produce another graph of the delta between line 1 and line 2 across intervals of delta across the full scale?
I want to avoid simply using the reading deltas because of the 'jumpy' graph that this meathod generates.
Many many thanks for any help,
Ed.

I believe what you want to do is interpolate between the unModNB and unModWB points and calculate the differences (deltas, as you say) between those. For that you can use interp1(). Try this:
k = 1000;
r = [100 220 470 1*k 2200 4700 10*k 22*k 47*k 100*k 220*k 470*k 1000*k ];
unModNB = [0.72 0.746 0.801 0.92 1.16 1.69 2.78 4.6 6.45 9.1 11.2 12.4 13.2];
unModWB = [1.124 1.17 1.23 1.48 1.84 2.65 4.2 7.6 11.8 15.4 18.6 20.01 21.7];
ModNBdB = 20*log10( unModNB );
ModWBdB = 20*log10( unModWB );
semilogx( r, ModNBdB, r, ModWBdB )
grid
legend('Line 1', 'Line 2')
sampled_x_vals = linspace(r(1),r(end),1000);
unModNB_sampled = interp1(r, unModNB, sampled_x_vals, 'pchip');
unModWB_sampled = interp1(r, unModWB, sampled_x_vals, 'pchip');
deltas = unModWB_sampled-unModNB_sampled;
figure, semilogx(sampled_x_vals,deltas), grid;
If, by 'deltas' you mean something other than difference, please clarify.

Related

How to normalize data by assigning NaN or zero to missing rows in matlab?

I use matlab 2014b.
I have a wrong program step. I don't know what is wrong with my script.
I have data per 10 minutes. time and value RR.
09/10/2014 3:00 0
09/10/2014 3:10 0
09/10/2014 3:30 0.4
09/10/2014 3:50 0.4
09/10/2014 4:00 0.4
09/10/2014 4:10 0.4
09/10/2014 4:20 0.4
09/10/2014 4:30 0.4
10/10/2014 4:40 0.4
09/10/2014 4:50 0.4
09/10/2014 5:00 0.4
09/10/2014 5:10 0.4
09/10/2014 5:20 0.4
....
Data consists of 12176x2 cell
It can be seen that after the second row there is no a time information nor data for 3:20 a.m. I want to get data per 10 minutes with empty data filled with 0 / NAN.
I use matlab 2014b and there is no retime function in that version.
I say thank you to anyone who has tried to help and advise.
times = out(:,1);
dn = datenum(times);
min_time = min(dn);
min_time_dv = datevec(min_time);
min_time_dv(5) = floor(min_time_dv(5) / 10) * 10;
first_slot_dn = datenum(min_time_dv);
max_time = max(dn);
max_time_dv = datevec(max_time);
max_time_dv(5) = floor(max_time_dv(5) / 10) * 10;
last_slot_dn = datenum(max_time_dv);
ten_mins_as_days = 1 / (24 * 60/10);
slot_dns = first_slot_dn : ten_mins_as_days : last_slot_dn;
slot_ds = datestr(slot_dns);
times_minutes = [cellstr(slot_ds(1:end,:))];
slot_ds = datestr(slot_dns);
[~, slot_idx] = histc(dn, slot_dns);
mean_RR1 = accumarray(slot_idx, RR(:), [length(slot_dns),1],#nanmean);
output1 = [cellstr(slot_ds(1:end,:)), num2cell(mean_RR1)];
I wrote a script and it has normalized well per 10 minutes. But the missing time was not filled by NAN. There was an error in normalizing the NAN values. Is there a suggestion to fix it?
I expect the results to be like this:
09/10/2014 3:00 0
09/10/2014 3:10 0
09/10/2014 3:20 NAN
09/10/2014 3:30 0.4
10/10/2014 3:40 NAN
I suggest using MATLAB's ismember function, which is very efficient in my experience.
time_input = ['09/10/2014 3:00'; '09/10/2014 3:10'; '09/10/2014 3:30'];
time_gaps = datetime(time_input,'InputFormat','dd/MM/yyyy H:mm');
L = length(time_gaps);
values_gaps = repelem(0.1,L);
% create normalized time vector
time_norm = datetime((time_gaps(1):minutes(10):time_gaps(end))');
% locate gaps by comparing the time vectors
[~, loc] = ismember(time_gaps,time_norm);
% create normalized value vector + fill at locations
values_norm = NaN(length(time_norm),1);
values_norm(loc) = values_gaps;
I did not bother using your code, as it was confusing to me. I use R2018b with datetime function, but it should be available to you in R2014b.

add tabs (spaces) to strings in plots for Octave / Matlab

How can I add tabs (spaces) to strings for plots in Octave see code below. It doesn't create a tab (There should be a tab between Signal and Max Freq in the plot)
Also it produces warning messages
warning: text_renderer: skipping missing glyph for character '9'
warning: called from
annotation>update_textbox at line 1080 column 11
annotation at line 248 column 7
clf
plot(0:0)
var=456
t1='Signal ';
t2=[char(9), 'Max Freq'];
t3=[char(10), 'nextline',num2str(var)];
str=strcat(t1,t2,t3);
annotation('textbox',...
[0.15 0.65 0.3 0.15],...
'String',{str},...
'FontSize',14,...
'FontName','Arial',...
'LineStyle','--',...
'EdgeColor',[1 1 0],...
'LineWidth',2,...
'BackgroundColor',[0.9 0.9 0.9],...
'Color',[0.84 0.16 0]);
Ps: I'm using Octave 4.2.2 on Ubuntu 18.04 64bit
I added t4 for blanks...doesn't look very nice. Also note I am using Matlab, not Octave so I didn't get your error. Not sure about that.
clf
plot(0:0)
var=456
t1='Signal ';
t4 = blanks(5);
t2=[char(9),t4, 'Max Freq'];
t3=[char(10), 'nextline',num2str(var)];
str=strcat(t1,t2,t3);
annotation('textbox',...
[0.15 0.65 0.3 0.15],...
'String',{str},...
'FontSize',14,...
'FontName','Arial',...
'LineStyle','--',...
'EdgeColor',[1 1 0],...
'LineWidth',2,...
'BackgroundColor',[0.9 0.9 0.9],...
'Color',[0.84 0.16 0]);

Printing Outputs Like table in Matlab

disp('iteration xl xu xr £a(%)');
xu=10;
xl=0;
xrpv=0;er=0;
f=#(x)(5*exp(0.5*x)+10-x^3.5);
for i=1:1:200;
xr=(xl+xu)/2;
fxr=f(xr);
er=((xr-xrpv)/xr)*100;
xrpv=xr;
if abs(er)<10^-6
disp(abs(er));
break
end
if (f(xl)*f(xr)>0)
xl=xr;
else
xu=xr;
end
fprintf('&d %f %f %f %f',i,xl,xu,xr,er)
end
i am trying the make table from outputs in for loop like;
xl xr xu ea%
0 5 10 100
1 * * *
2 * * *
An easy way to plot aligned text in the command window is to use tabs.
I recommend using fprintf for the header too.
I changed the first and 20th line:
fprintf('it. \t xl \t xu \t xr \t ea%%\n');
xu=10;
xl=0;
xrpv=0;er=0;
f=#(x)(5*exp(0.5*x)+10-x^3.5);
for i=1:1:200
xr=(xl+xu)/2;
fxr=f(xr);
er=((xr-xrpv)/xr)*100;
xrpv=xr;
if abs(er)<10^-6
disp(abs(er));
break
end
if (f(xl)*f(xr)>0)
xl=xr;
else
xu=xr;
end
fprintf('%3d \t%.1f \t%.1f \t%.1f \t%.1f\n',i,xl,xu,xr,er)
end
And the result is this:
it. xl xu xr ea%
1 0.0 5.0 5.0 100.0
2 2.5 5.0 2.5 -100.0
3 2.5 3.8 3.8 33.3
4 2.5 3.1 3.1 -20.0
5 2.5 2.8 2.8 -11.1
...
A similar result could be obtained by saving i,xl,xu,xr,er on each line of a matrix that could be displayed as a table.
You could also use a table variable as a storage variable that could be displayed as you asked but this depends on how you are using the data in the following code.

stan number of effective sample size

I reproduced the results of a hierarchical model using the rethinking package with just rstan() and I am just curious why n_eff is not closer.
Here is the model with random intercepts for 2 groups (intercept_x2) using the rethinking package:
Code:
response = c(rnorm(500,0,1),rnorm(500,200,10))
predicotr1_continuous = rnorm(1000)
predictor2_categorical = factor(c(rep("A",500),rep("B",500) ))
data = data.frame(y = response, x1 = predicotr1_continuous, x2 = predictor2_categorical)
head(data)
library(rethinking)
m22 <- map2stan(
alist(
y ~ dnorm( mu , sigma ) ,
mu <- intercept + intercept_x2[x2] + beta*x1 ,
intercept ~ dnorm(0,10),
intercept_x2[x2] ~ dnorm(0, sigma_2),
beta ~ dnorm(0,10),
sigma ~ dnorm(0, 10),
sigma_2 ~ dnorm(0,10)
) ,
data=data , chains=1 , iter=5000 , warmup=500 )
precis(m22, depth = 2)
Mean StdDev lower 0.89 upper 0.89 n_eff Rhat
intercept 9.96 9.59 -5.14 25.84 1368 1
intercept_x2[1] -9.94 9.59 -25.55 5.43 1371 1
intercept_x2[2] 189.68 9.59 173.28 204.26 1368 1
beta 0.06 0.22 -0.27 0.42 3458 1
sigma 6.94 0.16 6.70 7.20 2927 1
sigma_2 43.16 5.01 35.33 51.19 2757 1
Now here is the same model in rstan():
# create a numeric vector to indicate the categorical groups
data$GROUP_ID = match( data$x2, levels( data$x2 ) )
library(rstan)
standat <- list(
N = nrow(data),
y = data$y,
x1 = data$x1,
GROUP_ID = data$GROUP_ID,
nGROUPS = 2
)
stanmodelcode = '
data {
int<lower=1> N;
int nGROUPS;
real y[N];
real x1[N];
int<lower=1, upper=nGROUPS> GROUP_ID[N];
}
transformed data{
}
parameters {
real intercept;
vector[nGROUPS] intercept_x2;
real beta;
real<lower=0> sigma;
real<lower=0> sigma_2;
}
transformed parameters { // none needed
}
model {
real mu;
// priors
intercept~ normal(0,10);
intercept_x2 ~ normal(0,sigma_2);
beta ~ normal(0,10);
sigma ~ normal(0,10);
sigma_2 ~ normal(0,10);
// likelihood
for(i in 1:N){
mu = intercept + intercept_x2[ GROUP_ID[i] ] + beta*x1[i];
y[i] ~ normal(mu, sigma);
}
}
'
fit22 = stan(model_code=stanmodelcode, data=standat, iter=5000, warmup=500, chains = 1)
fit22
Inference for Stan model: b212ebc67c08c77926c59693aa719288.
1 chains, each with iter=5000; warmup=500; thin=1;
post-warmup draws per chain=4500, total post-warmup draws=4500.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
intercept 10.14 0.30 9.72 -8.42 3.56 10.21 16.71 29.19 1060 1
intercept_x2[1] -10.12 0.30 9.73 -29.09 -16.70 -10.25 -3.50 8.36 1059 1
intercept_x2[2] 189.50 0.30 9.72 170.40 182.98 189.42 196.09 208.05 1063 1
beta 0.05 0.00 0.21 -0.37 -0.10 0.05 0.20 0.47 3114 1
sigma 6.94 0.00 0.15 6.65 6.84 6.94 7.05 7.25 3432 1
sigma_2 43.14 0.09 4.88 34.38 39.71 42.84 46.36 53.26 3248 1
lp__ -2459.75 0.05 1.71 -2463.99 -2460.68 -2459.45 -2458.49 -2457.40 1334 1
Samples were drawn using NUTS(diag_e) at Thu Aug 31 15:53:09 2017.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at
convergence, Rhat=1).
My Questions:
the n_eff is larger using rethinking(). There is simulation differences but do you think something else is going on here?
Besides the n_eff being different the percentiles of the posterior distributions are different. I was thinking rethinking() and rstan() should return similar results with 5000 iterations since rethinking is just calling rstan. Are differences like that normal or something different between the 2 implementations?
I created data$GROUP_ID to indicate the categorical groupings. Is this the correct way to incorporate categorical variables into a hierarchical model in rstan()? I have 2 groups and if I had 50 groups I use the same data$GROUP_ID vector but is that the standard way?
Thank you.

Associated Labels in a dendrogram plot - MATLAB

I have the following set of data stored in file stations.dat :
Station A 305.2 321.1 420.9 383.5 311.7 197.1 160.2 113.9 60.5 60.5 64.8 154.3
Station B 281.1 304.0 353.1 231.9 84.6 20.9 11.7 11.9 31.1 75.8 133.0 235.3
Station C 312.3 342.2 366.2 335.2 200.1 74.4 45.9 27.5 24.0 53.6 87.7 177.0
Station D 402.2 524.5 554.9 529.5 347.5 176.8 120.2 35.0 12.6 13.3 14.0 61.6
Station E 261.3 262.7 282.3 232.6 103.8 33.2 16.7 33.2 111.0 149.0 184.8 227.0
By using the following commands,
Z = linkage (stations.data,'ward','euc');
figure (1), dendrogram(Z,0,'orientation', 'right')
I get the figure below:
So cluster 1 components are 4,3,1 (Stations D,C and A, respectively) and cluster 2 are 5,2(Stations E and B).
I want to put the name of Stations on plot, but if I use the command:
set (gca,'YTickLabel', stations.textdata);
The figure I get is the following:
How can I associate data to respective names and plot in dendrogram.
I have 144 stations data. I used only 5 for illustration.
Try the following:
ind = str2num(get(gca,'YTickLabel'));
set(gca, 'YTickLabel',stations.textdata(ind))
An easier way would be to specify the labels of the data points in the dendrogram call directly:
dendrogram(Z,0, 'Orientation','right', 'Labels',stations.textdata)