Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // PulsatorTestVC.swift
- // Vadia3
- //
- // Created by Jan Ziółkowski on 22.08.2018.
- // Copyright © 2018 Jan Ziółkowski. All rights reserved.
- //
- import UIKit
- import Foundation
- class PulsatorTestVC: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- }
- public func testPulsator(_ channelsData: [[Double]], _ channelsDataTime: [Double], _ channels: inout [Int]) -> PulsatorTestDto? { //ms
- var result = PulsatorTestDto()
- var channelPeakCounter = 0
- for i in 0..<4 {
- if !channels.contains(i + 1) {
- continue
- }
- let channel = i
- var channelData = channelsData[channel]
- var peakCounter = 0
- var peak = false
- let loopStartIndex = GetPeakIndex(channelData)
- // not enough data to create proper test
- if channelData.count < loopStartIndex + 1400 || loopStartIndex == -1 { //?
- // return nil
- continue
- }
- // 7 sec from startDate
- for k in 0..<loopStartIndex + 1400 {
- // border value for peak at kPa
- if channelData[k] > 40 {
- peak = true
- }
- // border value for bottom at kPa
- if channelData[k] < 2 && peak == true {
- peakCounter += 1
- peak = false
- // 6 peaks is minimum to create a proper test
- if peakCounter > 5 {
- channelPeakCounter += 1
- break
- }
- }
- }
- var channelResult = TestPulsatorChannel(channelData)
- if channelPeakCounter < 1 {
- channelResult = nil
- }
- if channelResult != nil {
- channelResult!.Scale = ""
- channelResult = CountPulsatorValue(channelResult!)
- channelResult!.startTestTime = channelsDataTime[loopStartIndex]
- channelResult!.ChannelNumber = channel + 1
- result.PulsatorTestChannelList.append(channelResult!)
- result = CountLimp(result)
- }
- }
- return result
- }
- private func GetPeakIndex(_ channelData: [Double]) -> Int {
- for i in 0..<channelData.count {
- if channelData[i] > 40 {
- return i
- }
- }
- return -1
- }
- private func TestPulsatorChannel(_ channelData: [Double]) -> PulsatorTestChannelDto? {
- let cyclesToMeasure = 5
- let phases = GetPhases(channelData, cyclesToMeasure)
- if phases == nil {
- return nil
- }
- let result = GetPulsatorChannelResult(phases!, cyclesToMeasure)
- return result
- }
- private func GetPulsatorChannelResult(_ phases: [PulsatorTestChannelDto], _ cyclesToMeasure: Int) -> PulsatorTestChannelDto {
- let item = PulsatorTestChannelDto()
- item.TimeA = phases.map {$0.TimeA}.reduce(0, +) / Double(phases.count)
- item.TimeB = phases.map {$0.TimeB}.reduce(0, +) / Double(phases.count)
- item.TimeC = phases.map {$0.TimeC}.reduce(0, +) / Double(phases.count)
- item.TimeD = phases.map {$0.TimeD}.reduce(0, +) / Double(phases.count)
- item.Vmax = phases.map {$0.Vmax}.reduce(0, +) / Double(phases.count)
- // item.Vmax = ScaleCalculate(item.Vmax)
- let sum = item.TimeA + item.TimeB + item.TimeC + item.TimeD
- if sum != 0 {
- item.Rate = round(60000 / sum)
- item.PercentA = round(item.TimeA * 100 / sum)
- item.PercentB = round(item.TimeB * 100 / sum)
- item.PercentC = round(item.TimeC * 100 / sum)
- item.PercentD = round(item.TimeD * 100 / sum)
- }
- else {
- item.Rate = 0
- item.PercentA = 0
- item.PercentB = 0
- item.PercentC = 0
- item.PercentD = 0
- }
- item.Ratio1 = item.PercentA + item.PercentB
- item.Ratio2 = 100 - (item.PercentA + item.PercentB)
- return item
- }
- private func GetPhases(_ channelData: [Double]?, _ cyclesToMeasure: Int) -> [PulsatorTestChannelDto]? {
- if channelData == nil {
- return [PulsatorTestChannelDto]()
- }
- var index = 0
- var phases = [PulsatorTestChannelDto]()
- for _ in 0..<cyclesToMeasure {
- let startA = GetNextAPhaseStart(index, channelData!)
- let startD = GetNextDPhaseStart(startA, channelData!)
- let vMax = GetVmax(startA, startD, channelData!)
- let startB = GetNextBPhaseStart(startA, startD, channelData!, vMax)
- let startC = GetNextCPhaseStart(startB, startD, channelData!, vMax)
- let endD = GetNextDPhaseEnd(startD, channelData!)
- // print("startA: \(startA) startB: \(startB) startC: \(startC) startD: \(startD) \n")
- let item = PulsatorTestChannelDto()
- item.StartA = startA
- item.StartB = startB
- item.StartC = startC
- item.StartD = startD
- item.EndD = endD
- if [item.StartA, item.StartB, item.StartC, item.StartD, item.EndD].contains(where: { $0 == -1 }) {
- return nil
- }
- item.TimeA = Double((startB - startA) * 5)
- item.TimeB = Double((startC - startB) * 5)
- item.TimeC = Double((startD - startC) * 5)
- item.TimeD = Double((endD - startD) * 5)
- item.Vmax = vMax
- index = startD
- phases.append(item)
- }
- for i in 0..<cyclesToMeasure - 1 {
- for j in stride(from: Int(phases[i].StartB + 5), to: Int(phases[i].StartC - 5), by: 1) {
- if abs(channelData![j] - (phases[i].Vmax * 10)) > 40 {
- phases[i].AnomalyCounterB += 1
- }
- }
- for j in stride(from: Int(phases[i].StartD + 5), to: Int(phases[i + 1].StartA - 5), by: 1) {
- if channelData![j] > 40 {
- phases[i].AnomalyCounterD += 1
- }
- }
- }
- return phases
- }
- private func GetNextDPhaseEnd(_ startD: Int, _ channelData: [Double]) -> Int {
- if startD == -1 {
- return -1
- }
- for i in stride(from: startD, to: channelData.count, by: 1) {
- if channelData[i] > 4 {
- return i
- }
- }
- return -1
- }
- private func GetNextCPhaseStart(_ startB: Int, _ startD: Int, _ channelData: [Double], _ vMax: Double) -> Int {
- for i in stride(from: startB, to: startD, by: 1) {
- if channelData[i] < vMax - 4 {
- return i
- }
- }
- return -1
- }
- private func GetNextBPhaseStart(_ startA: Int, _ startD: Int, _ channelData: [Double], _ vMax: Double) -> Int {
- for i in stride(from: startA, to: startD, by: 1) {
- if channelData[i] > vMax - 4 {
- return i
- }
- }
- return -1
- }
- private func GetVmax(_ startA: Int, _ startD: Int, _ channelData: [Double]) -> Double {
- var max = -1 as Double
- for i in stride(from: startA, to: startD, by: 1) {
- if max < channelData[i] {
- max = channelData[i]
- }
- }
- return max
- }
- private func GetNextDPhaseStart(_ startA: Int, _ channelData: [Double]) -> Int {
- if startA == -1 {
- return -1
- }
- for i in startA..<channelData.count {
- if channelData[i] < 4 {
- return i
- }
- }
- return -1
- }
- private func GetNextAPhaseStart(_ index: Int, _ channelData: [Double]) -> Int {
- var next = false
- if index == -1 {
- return -1
- }
- for var i in index..<channelData.count {
- if channelData[i] < 4 {
- next = true
- }
- if next && channelData[i] > 4 {
- return i
- }
- i += 1
- }
- return -1
- }
- private func CountLimp(_ result: PulsatorTestDto) -> PulsatorTestDto {
- var channels = result.PulsatorTestChannelList
- var percent = Array(repeating: Double(), count: 4)
- for i in 0..<channels.count {
- let aib0 = channels[i].TimeA + channels[i].TimeB
- let sum0 = channels[i].TimeA + channels[i].TimeB + channels[i].TimeC + channels[i].TimeD
- percent[i] = (aib0 * 100) / sum0
- }
- let limp = abs(percent.max()! - percent.min()!)
- for i in 0..<channels.count {
- result.PulsatorTestChannelList[i].Limp = round(limp)
- }
- return result
- }
- public func CountPulsatorValue(_ channel: PulsatorTestChannelDto) -> PulsatorTestChannelDto {
- if channel.TimeA < 0 {
- channel.TimeA = 0
- }
- if channel.TimeB < 0 {
- channel.TimeB = 0
- }
- if channel.TimeC < 0 {
- channel.TimeC = 0
- }
- if channel.TimeD < 0 {
- channel.TimeD = 0
- }
- let sum = channel.TimeA + channel.TimeB + channel.TimeC + channel.TimeD
- if sum > 0 {
- channel.PercentA = (channel.TimeA / sum) * 100
- channel.PercentB = (channel.TimeB / sum) * 100
- channel.PercentC = (channel.TimeC / sum) * 100
- channel.PercentD = (channel.TimeD / sum) * 100
- channel.Ratio1 = channel.PercentA + channel.PercentB
- channel.Ratio2 = 100 - channel.Ratio1
- channel.Dip = 0
- channel.Rate = 60 / (sum / 1000)
- }
- if channel.AnomalyCounterB > 0 {
- channel.Dip = Double(channel.AnomalyCounterB)
- }
- else if channel.AnomalyCounterD > 0 {
- channel.Dip = Double(channel.AnomalyCounterD)
- }
- return channel
- }
- public enum Error { //?
- case None
- case NotEnoughData
- case WrongChannelsNumber
- }
- private func ScaleCalculate(_ inValue: Double) -> Double { // ?
- if UserDefaults.standard.integer(forKey: "vacuumUnits") == 0 {
- return inValue
- }
- return 0.295299831 * inValue
- }
- //
- // public func GetData(_ start: Date, _ channel: Int, _ startDate: inout Date) -> [Double] {
- // startDate = start
- // if vadiaData[0].time > start {
- // startDate = vadiaData[0].time
- // }
- //
- // return GetData(start, channel)!
- // }
- //
- // public func GetData(_ start: Date, _ channel: Int) -> [Double]? {
- // if channel < 0 || channel > 3 {
- // return nil
- // }
- // var data = vadiaData.filter {$0.time >= start}
- //// var resultCount = data.count * 200
- // var result = [Double]()
- //
- // for i in 0..<data.count {
- // result.append(contentsOf: data[i].channelsList[channel].prefix(200))
- // }
- //
- // return result
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement