在 R 中读取 LTSpice .step 数据

Read LTSpice .step data in R

我正在尝试解析使用 .step 函数生成的 LTSpice 数据。这会将生成的数据集拆分成我不知道如何使用 R.

有效处理的部分
library(tidyverse)
data <- read_tsv("FileName")
## 
## ── Column specification ────────────────────────────────────────────────────────
## cols(
##   time = col_character(),
##   `V(out)` = col_double(),
##   `V(in)` = col_double()
## )
## Warning: 24 parsing failures.
## row col  expected    actual                           file
##   1  -- 3 columns 1 columns 'FileName'
##   6  -- 3 columns 1 columns 'FileName'
##  11  -- 3 columns 1 columns 'FileName'
##  16  -- 3 columns 1 columns 'FileName'
##  21  -- 3 columns 1 columns 'FileName'
## ... ... ......... ......... ..............................
## See problems(...) for more details.
head(data)
## # A tibble: 6 x 3
##   time                                                          `V(out)` `V(in)`
##   <chr>                                                            <dbl>   <dbl>
## 1 Step Information: Resistor1=10 Resistor2=1M Offset=-3  (Run…  NA       NA     
## 2 0.000000000000000e+00                                         -1.50e-5  0     
## 3 4.913626562387649e-09                                          3.08e-2  0.0309
## 4 4.996634374887648e-09                                          3.13e-2  0.0314
## 5 5.000000000000000e-09                                          3.14e-2  0.0314
## 6 Step Information: Resistor1=10.01K Resistor2=1M Offset=-3  …  NA       NA

如您所见,数据被分成几部分,这些部分由有关步骤的信息分隔。我想更改数据,删除包含“步骤信息”的行,并将相应的参数写入匹配的列:

## # A tibble: 6 x 6
##   time                    `V(out)` `V(in)` Resistor1 Resistor2 Offset
##   <chr>                      <dbl>   <dbl> <lgl>     <lgl>     <lgl> 
## 1 0.000000000000000e+00 -0.0000150  0      NA        NA        NA    
## 2 4.913626562387649e-09  0.0308     0.0309 NA        NA        NA    
## 3 4.996634374887648e-09  0.0313     0.0314 NA        NA        NA    
## 4 5.000000000000000e-09  0.0314     0.0314 NA        NA        NA    
## 5 0.000000000000000e+00 -0.00749    0      NA        NA        NA    
## 6 4.908743749887649e-09  0.00789    0.0308 NA        NA        NA

编辑:

部分原始数据:

time    V(out)  V(in)
Step Information: Resistor1=10 Resistor2=1M Offset=-3  (Run: 1/24)
0.000000000000000e+00   -1.498494e-05   0.000000e+00
4.913626562387649e-09   3.082234e-02    3.086832e-02
4.996634374887648e-09   3.134312e-02    3.138962e-02
5.000000000000000e-09   3.136424e-02    3.141076e-02
Step Information: Resistor1=10.01K Resistor2=1M Offset=-3  (Run: 2/24)
0.000000000000000e+00   -7.485015e-03   0.000000e+00
4.908743749887649e-09   7.887614e-03    3.083766e-02
4.996634374887648e-09   8.162769e-03    3.138962e-02
5.000000000000000e-09   8.173305e-03    3.141076e-02
Step Information: Resistor1=20K Resistor2=1M Offset=-3  (Run: 3/24)
...

它总是有点难看,但我认为您最好使用原始数据。拉入线,标记有步骤信息的行,将数据分离出来,然后合并:

## read everything in as raw lines
## for this example use this code
##    tmp <- readLines(textConnection(txt))
## for your real data use this code
tmp <- readLines("FileName")

## flag the Step Information rows
stl <- grepl("^Step", tmp)

## get all name=value pairs out of the Step Information rows and join together
nms <- regmatches(tmp[stl], gregexpr("\S+(?=\=)",    tmp[stl], perl=TRUE))
val <- regmatches(tmp[stl], gregexpr("(?<=\=)(\S+)", tmp[stl], perl=TRUE))
nvp <- do.call(rbind, Map(function(v,n) as.data.frame(as.list(v), col.names=n), val, nms))

## finally put it all together
cbind(
  read.table(text=tmp[!stl], header=TRUE),
  nvp[ cumsum(stl)[!stl][-1], ]
)

#            time        V.out.      V.in. Resistor1 Resistor2 Offset
#1   0.000000e+00 -1.498494e-05 0.00000000        10        1M     -3
#1.1 4.913627e-09  3.082234e-02 0.03086832        10        1M     -3
#1.2 4.996634e-09  3.134312e-02 0.03138962        10        1M     -3
#1.3 5.000000e-09  3.136424e-02 0.03141076        10        1M     -3
#2   0.000000e+00 -7.485015e-03 0.00000000    10.01K        1M     -3
#2.1 4.908744e-09  7.887614e-03 0.03083766    10.01K        1M     -3
#2.2 4.996634e-09  8.162769e-03 0.03138962    10.01K        1M     -3
#2.3 5.000000e-09  8.173305e-03 0.03141076    10.01K        1M     -3

我使用的示例数据是:

txt <- "time\tV(out)\tV(in)
Step Information: Resistor1=10 Resistor2=1M Offset=-3  (Run: 1/24)
0.000000000000000e+00\t-1.498494e-05\t0.000000e+00
4.913626562387649e-09\t3.082234e-02\t3.086832e-02
4.996634374887648e-09\t3.134312e-02\t3.138962e-02
5.000000000000000e-09\t3.136424e-02\t3.141076e-02
Step Information: Resistor1=10.01K Resistor2=1M Offset=-3  (Run: 2/24)
0.000000000000000e+00\t-7.485015e-03\t0.000000e+00
4.908743749887649e-09\t7.887614e-03\t3.083766e-02
4.996634374887648e-09\t8.162769e-03\t3.138962e-02
5.000000000000000e-09\t8.173305e-03\t3.141076e-02"