"efromthresh.wavelet" <- function(x.dwt, vscale = NA) { # # Carry out the Efromovich smoothing approach for the wavelet transform # xdwt, produced using the # S+Wavelets module. Carry out discrete wavelet transform first. # # # nlevs is the number of levels of detail computed within the # transform. The detail is then held in vectors which are # accessible as x.dwt[[2]], x.dwt[[3]], ... , x.dwt[[nlevs+1]], # with x.dwt[[2]] being the coarsest level and x.dwt[[nlevs+1]] # the finest. # nlevs <- attributes(x.dwt)$n.levels nn <- 2 * length(x.dwt[[nlevs + 1]]) xrecon <- matrix(0, nrow = nn, ncol = nlevs) nnonzero <- rep(0, nlevs) intsq <- rep(0, nlevs) if(is.na(vscale)) { vscale <- mad(x.dwt[[nlevs + 1]]) } # carry out the algorithm for every possible choice of cutoff for(cutoff in (1:nlevs)) { xnew.dwt <- x.dwt # # Deal first with the coarse levels and with scaling coefficients # for(j in (1:(cutoff + 1))) { xx <- x.dwt[[j]]^2 fac <- rep(0, length(xx)) ind <- (xx > vscale^2) fac[ind] <- (xx[ind] - vscale^2)/xx[ind] xnew.dwt[[j]] <- fac * x.dwt[[j]] } if(cutoff < nlevs) for(j in (1:(nlevs - cutoff))) { jj <- nlevs + 2 - j kind <- 2 * (nlevs - cutoff - j) + 1 xdabs <- abs(x.dwt[[jj]]) thr1 <- quantile(xdabs, 1 - 2^( - kind)) thr2 <- vscale * min(2^(jj - cutoff), sqrt(2 * log(nn))) thr <- max(thr1, thr2) xnew.dwt[[jj]][xdabs < thr] <- 0 } xrecon[, cutoff] <- reconstruct(xnew.dwt) nnonzero[cutoff] <- sum(xnew.dwt != 0) intsq[cutoff] <- sum(xrecon[, cutoff]^2) } # # now find the best reconstruction according to Efrom rule, # corrected by rescaling of variance term # efind <- 2 * vscale^2 * nnonzero - intsq jmin <- (1:nlevs)[efind == min(efind)] x <- xrecon[, jmin] browser() return(x) } "simulation3.efrom" <- function(nreps = 100) { # # Carries out simulation on the same data as simulation2, using the Efromovich # approach # results <- array(NA, dim = c(4, 2, nreps)) signals <- c("bumps", "blocks", "doppler", "heavisine") snrnames <- c("High", "Low") snrs <- c(3, 7) dimnames(results) <- list(signals, snrnames, NULL) for(i in (1:4)) { for(j in (1:2)) { set.seed(153) ztrue <- make.signal(signals[i]) ztrue <- (snrs[j] * ztrue)/sqrt(var(ztrue)) errs <- rep(NA, nreps) for(jrep in (1:nreps)) { zdata <- rnorm(1024, ztrue) results[i, j, jrep] <- (vecnorm(efromthresh.wavelet(dwt(zdata)) - ztrue))^2 cat(".") } cat("+") } } return(results) } "efromres" <- new("array", .Data = c(321.425977698892, 303.393856019843, 152.921910504278, 51.0062009819067, 384.475436576176, 349.624375460901, 238.605939113033, 71.9845188959306, 298.169969494794, 302.528110028968, 161.637645519401, 54.2794377580798, 372.679680399831, 274.232459118934, 228.542548737202, 79.3914923533031, 398.380210383481, 367.752212256212, 172.913785267394, 50.7636581721564, 426.038499580686, 402.0643545557, 246.147681526495, 105.273526314095, 337.521737060378, 290.602866972624, 154.534027174131, 56.8635500377397, 380.470380554287, 342.52471091427, 280.570354886697, 108.578710627779, 342.55593098373, 306.661078847041, 177.21063888969, 42.22366213145, 395.060897041847, 351.590216898342, 249.797000351396, 64.7213974945521, 398.458592934753, 348.182967026828, 168.947499296847, 49.2528149916042, 564.97854672355, 421.741873876112, 242.560001090142, 61.2081173108097, 402.802142364244, 320.390001366142, 186.200032637334, 63.9123811760755, 327.633068722895, 390.812827243554, 210.514384366744, 98.668171440286, 273.113539017808, 269.268799054817, 167.832141283913, 44.044249891417, 398.285556446893, 340.612799266439, 227.813775234632, 81.8323839206318, 277.540663255431, 231.001764223912, 150.290616576995, 41.5113966088306, 323.228326118131, 306.251584395894, 215.220922679725, 78.4304876382632, 432.126319161219, 295.398370832437, 181.191369230582, 57.8464583922989, 398.132289769425, 368.125584523694, 240.11090394849, 56.3724541151019, 468.117135059219, 360.294176560263, 157.601707019665, 50.991581102996, 388.976550956822, 362.00750421807, 251.120954126975, 80.4852691966081, 344.703737888675, 291.347057276269, 165.635839235501, 51.1169702407283, 401.100369062627, 414.773341750102, 216.509934361168, 59.7356854045786, 350.512789089397, 316.687054670691, 180.98679090666, 61.1275044852935, 420.360713144943, 386.398848893387, 235.998014803995, 86.4110123723732, 451.175444149982, 337.371355001039, 198.743167414074, 57.2248042463001, 425.770506018816, 419.334017594389, 229.154069481024, 77.0793893319367, 495.058129103235, 322.655076283418, 231.093498942302, 94.9151521779708, 456.438163479254, 412.947068378289, 315.319440516094, 116.640783307396, 409.223360623435, 301.412981361326, 209.085374737953, 56.837220093078, 387.762081542129, 330.876792161778, 229.773356251263, 64.6001526293044, 366.152279770086, 259.3057428783, 144.247135610154, 55.5169217100199, 399.177006008603, 327.48504489557, 242.653614924447, 73.8264683343648, 433.711905485101, 412.541567552144, 171.952865978009, 51.9273221244331, 414.687257177926, 424.172102804305, 242.117458650614, 68.847299130783, 362.0507457034, 324.62480492658, 176.930480154713, 45.8851569579485, 349.430245906928, 386.619184680472, 198.774105384076, 58.3212960952661, 418.602114604201, 275.698923049119, 187.72022556138, 55.9203087845554, 429.914218177789, 345.356442059553, 237.364464034033, 71.1049361298212, 309.478483312437, 313.533067691923, 174.186584532118, 54.4594334529455, 421.682791262156, 383.597065770386, 232.342034261226, 73.4015660512929, 306.38758829562, 371.765878105111, 200.827550536422, 86.015617622507, 450.97597693865, 408.203019424644, 220.05852924051, 136.091862550223, 376.031165278905, 329.487834064249, 164.092172085971, 41.8865475869856, 387.642899440306, 326.951296525739, 256.249618094176, 77.3078107736563, 338.146566899287, 220.493123898833, 154.421648170321, 61.1293828845422, 343.277165288646, 321.800361135796, 207.910062210488, 67.9830623407776, 488.088831457079, 282.797124489785, 161.801856114879, 46.4761316894211, 505.5670206882, 366.062050675754, 287.554079864686, 70.4455358197985, 348.720818939132, 289.670048889862, 147.55606962977, 45.7704254306221, 373.740619654982, 320.323398034046, 253.169663731187, 73.6844366926939, 355.672376564647, 268.523977336409, 137.678943131845, 93.5590281508439, 355.551797172238, 331.511688294414, 224.106934341873, 93.4247408473804, 353.301187913017, 305.342339813845, 199.870447478383, 64.3935988867425, 360.485109780356, 378.496098211527, 296.637436594789, 84.338469118306, 307.755206135749, 306.955248695675, 156.277443939891, 58.0756457354529, 431.066792061735, 362.752083497492, 234.068066866952, 71.605227947765, 348.743655609257, 312.35503562996, 220.996924569437, 86.4005915074941, 429.171474822944, 312.747373625345, 256.739301879442, 110.022846565161, 323.226699537406, 369.795983634235, 177.722011388794, 58.6315505238595, 425.312263352923, 331.265918727655, 181.405849877853, 61.5968440583825, 381.816193011309, 292.954483207221, 157.175050077329, 47.2740979077609, 394.58807594862, 413.743017535857, 232.258574802802, 78.2351055182928, 325.943040472968, 254.039246588748, 164.631937152321, 53.3216799183428, 356.508871786666, 318.756196287918, 212.416992451326, 69.9289850306944, 354.601577950109, 364.457129231452, 165.461662445524, 57.1411551304276, 427.517573031282, 353.889835476548, 270.611230825925, 95.5445321057689, 312.15951397597, 237.292715216626, 188.918887636067, 57.3300850295196, 423.405723124478, 350.828856717416, 216.099497889363, 71.4932434933134, 320.927320706687, 296.378872531394, 184.76007746725, 48.8492028545952, 393.725286529895, 326.740177888272, 229.332329791279, 76.38674902983, 359.692064245882, 304.519957225967, 182.013108068132, 51.1317515576537, 463.595617065204, 346.417254645889, 194.401793006148, 66.2287967058943, 288.792047782275, 283.674292415496, 176.696365398636, 68.6425877136724, 367.377493419644, 368.152527573498, 232.896795494027, 83.5683435754954, 419.910956702625, 396.987181857572, 200.951351594173, 70.5502275645356, 458.691917599062, 357.408800360779, 314.753618450784, 98.0943411856246, 353.021538833325, 330.054120371068, 159.154634399888, 55.6040028739215, 553.625650464568, 382.121448212445, 245.930141516498, 76.5605996838153, 346.763368075007, 302.834967668355, 195.005855079178, 83.783746619292, 373.074539131629, 345.773209529016, 253.750967581942, 105.196125662022, 376.824369079205, 309.120135276883, 191.930684043016, 82.5767457510513, 407.497774035508, 421.156679455863, 229.58111794855, 111.345801819639, 366.383897984912, 286.619518084538, 185.814197498074, 60.6139232257855, 434.704577407525, 390.204801598307, 251.599805421217, 111.407503131193, 314.728983083465, 338.786064975415, 164.925035346447, 48.0074547302115, 426.503981139416, 414.003088092618, 237.474197632685, 83.4829879720208, 421.932135446319, 313.723742168318, 219.512447009536, 88.3105762051552, 446.562698276018, 392.188808158981, 309.389787640245, 84.4272850684182, 362.288153405923, 295.274781694841, 190.953921160148, 62.3944772801891, 444.136530147286, 355.753388209643, 234.99418134765, 83.0905401356363, 317.410721883624, 281.96714715037, 142.164423839492, 44.5327696994223, 426.317926488075, 386.823615282727, 262.743192856022, 77.8616945416749, 359.106493650438, 312.442965307178, 170.750946587203, 49.4604543515579, 376.45387440691, 343.574824794158, 266.103692257118, 63.3587588257145, 395.557272414301, 289.89506508133, 155.737411930093, 62.358349750777, 513.032491246263, 353.716319323302, 234.934209055867, 65.4010249925078, 336.588125501151, 300.194999831233, 176.935527016122, 43.9472586280005, 317.640639396354, 360.878965615783, 237.190700801725, 73.2520306537146, 373.758618234625, 328.248463887847, 197.544105581103, 99.7946786098777, 449.914495577945, 421.034554158864, 232.434642566439, 94.6501049471426, 372.213078185012, 311.226508412938, 184.949799303206, 76.3507464317254, 412.263925680925, 422.982828172674, 262.885038464707, 94.2255736856935, 357.191211279094, 354.879124936992, 151.166795831987, 43.9378054648339, 413.896607339983, 352.114257266121, 196.450597238183, 84.8126944542208, 307.47423223271, 297.602520491417, 231.541062433986, 62.7182795816122, 454.095233350931, 361.678141487396, 267.441241065373, 81.9958716133946, 354.712993182798, 258.440177027076, 191.383687312538, 46.1335630322251, 368.632158313959, 363.122284987675, 225.022618156649, 60.1875445739548, 334.908101254192, 287.450938793576, 153.609054626516, 53.0084358446492, 438.29385123461, 378.610074996109, 226.888504355594, 65.4794061002123, 334.531681536782, 381.826800283533, 173.541862792638, 68.5643607486709, 382.937658310054, 392.756291686479, 280.263034076961, 105.418093219461, 334.04376264255, 286.477183115771, 195.757893068383, 59.4144326015324, 411.377373964677, 400.327605878623, 238.815608706853, 82.1790814087563, 407.352902146728, 340.149730353474, 191.832376651028, 52.3914599504939, 436.866066682749, 343.395188282577, 226.526955105695, 62.5065277451927, 349.086371199961, 280.043709805136, 176.091305709208, 66.1487097549427, 436.770225886287, 376.207200418667, 254.879418535766, 109.565649608484, 355.210422074684, 311.536231501626, 176.133185794803, 57.1551961540657, 433.5074662718, 397.724483782729, 282.640978703482, 74.7321964034767, 368.356340670437, 343.425984142508, 159.145757261177, 55.5992096250009, 452.988576044792, 371.670443500863, 260.909878218359, 68.5960042892092, 368.129460335269, 300.849794100602, 168.326450708661, 69.4004970429463, 394.698047806239, 321.161770966652, 215.957461773203, 77.058239147055, 375.062651351702, 381.704111821053, 176.869425803696, 55.0358566676686, 430.779218418225, 318.011200932642, 206.004520012651, 79.4088216135141, 488.048839975361, 348.576243648897, 212.669525689069, 70.9900934182613, 492.187443701314, 436.649276008791, 253.736767622323, 105.546492637623, 341.86014616085, 334.202482366815, 177.515955563209, 74.4326124513041, 457.750835327447, 366.840543260376, 272.802367380059, 120.98629321054, 461.272898904738, 283.337464858237, 175.759574469421, 63.607260730628, 431.078790704292, 353.828926011881, 259.442444102128, 90.8813648936609, 425.389574903031, 316.69372067313, 183.76655812115, 48.0479993872493, 383.851899209916, 391.53316387644, 259.9283888708, 97.5457151409289, 315.014347653484, 258.669058910612, 163.464668040614, 48.0803763219979, 326.826480382675, 358.335851731263, 226.192433355977, 69.5060177113043, 374.199065719946, 348.718836465849, 187.192905175229, 51.861529112406, 436.360056462153, 348.086298439744, 256.124843167553, 80.7241388107511, 409.558555274738, 383.572084765097, 172.059722031806, 51.6476897005156, 515.708862180714, 408.966350908229, 265.397434364037, 110.5495314851, 342.50211122599, 394.998058138443, 159.07935312407, 49.6982872219574, 408.146968129119, 349.87070082699, 213.917548449467, 52.16550419627, 324.991253118232, 319.006601696413, 156.037377524288, 53.1440927500659, 494.110384148695, 386.174265577842, 258.985030747536, 71.9389688146498, 428.114864364536, 303.314879913332, 171.334788287002, 55.4404281355292, 410.031164915008, 417.624683224344, 244.329813143793, 81.0025662725975, 338.583202557002, 300.436200984003, 173.371638566885, 49.7649740167868, 381.758950332221, 376.987480215745, 248.544940599758, 88.5602075932264, 323.743561780084, 285.211444306696, 172.707252277775, 47.3853442646911, 369.450641170443, 367.840663016039, 262.268102118747, 80.0522756663209, 301.858083675589, 254.309081860233, 166.096238320508, 47.1510315993744, 331.869284293474, 312.510301861187, 247.63541365556, 73.1348574891362, 300.708733866528, 286.703782675234, 182.494575150929, 51.7928623036889, 399.549806884414, 329.7804580886, 216.449977967031, 62.1600698924344, 300.91919200618, 303.683216536568, 173.351490185034, 51.288270132967, 389.804424772294, 367.508523793811, 232.072545464254, 72.1699475797078, 400.246956650084, 414.24832150834, 174.031661951773, 51.8478649243863, 443.627455448499, 434.982593435568, 264.453790721535, 75.4541304559925, 359.27122673262, 325.748262191416, 174.370651486566, 66.6474185531731, 452.616584215042, 380.27940303912, 227.069182222176, 95.2702168425729, 328.762372209348, 277.066298429892, 169.979482578823, 48.2491071300668, 399.155390444468, 364.709031984914, 202.217044559861, 84.1962634983215, 317.670014762063, 358.90585760229, 128.829977465033, 46.1143235842135, 397.17692833341, 368.809856444728, 188.674973856048, 60.0079581785898, 411.309750619265, 362.607678560857, 214.214126273814, 48.1509778497567, 382.751802113963, 395.332392790577, 277.998986277327, 69.4163565886577, 433.713314514211, 308.869423009291, 160.336182792096, 57.044458827982, 465.380168257256, 332.093078893296, 280.599089481443, 86.7659043032969, 302.46109028024, 270.327817795068, 163.404772006268, 64.1297161752612, 407.995146354308, 318.495461707832, 236.419462276256, 105.517322643795, 351.322605527404, 312.065417096779, 156.884329508382, 51.0184759356331, 378.674297076728, 375.386575191401, 215.184824901839, 72.1231757020701, 352.882886753036, 287.034391030682, 177.652100971173, 69.3048577364637, 428.014514276629, 381.218241804628, 310.790816891467, 119.92010409045, 366.099335613264, 265.91303298678, 174.271869772426, 57.8765831526731, 416.753285173832, 346.733922326795, 243.826305849701, 57.6198941728342, 361.608555369901, 338.196314456386, 169.17837174758, 64.5443109570942, 386.242338798595, 394.16211161025, 229.780083131588, 91.8454184734556, 357.720620938794, 239.154441756736, 189.376989705002, 52.6153551162588, 363.917520804766, 333.750515244756, 242.745644345992, 94.5715435160333, 311.805552194466, 311.493259844247, 181.714387082596, 63.3843043371023, 360.175569336359, 347.101312354988, 227.990269828562, 73.9069595467957, 336.58237476223, 255.704397003437, 193.37797127573, 69.5608798640242, 407.1012440225, 353.689634723528, 275.054161251288, 96.9750571783781, 392.019186532007, 283.261379135316, 133.157340936495, 48.778096309751, 390.939570676849, 384.529373295418, 217.330413073414, 62.7929330057535, 318.355107077771, 261.713958639239, 168.071637411217, 90.3246092346814, 393.67194149524, 366.671477634701, 227.992896079178, 112.8065725474, 293.18878966073, 355.446316703427, 162.314438488553, 42.9612726083565, 360.855344980458, 295.185410550307, 281.035171399027, 63.4512849168213, 278.676564277688, 263.070158705969, 176.412766543975, 44.0542881732776, 371.893956775662, 298.252925706277, 243.937323860452, 59.2982343023721, 350.729526533065, 264.15811688616, 156.422330305079, 55.3366841468848, 427.42017888122, 371.063156770652, 233.422296591394, 62.6283872196284, 301.771654285384, 341.669634787124, 198.244979160979, 52.1126541125293, 427.674326953616, 405.089018780205, 258.201958723184, 81.9461578122503, 325.355142752364, 335.501351895304, 203.779511433244, 90.9631287435914, 383.54486278386, 365.764680458721, 260.230331425906, 116.914366539425) , .Dim = c(4, 2, 100) , .Dimnames = list(c("bumps", "blocks", "doppler", "heavisine") , c("High", "Low") , character(0) ) )