1

my code does not print in the console. I have tried a few methods but nothing is working. The code below is the full version of the code that might lead to the reason why the print is not showing the in console. Please help me to identify this minor issue. I have tried using as many methods as possible to debug this issue but to no avail.

This is my code:

import Foundation

// Function to calculate the arithmetic mean

func arithmeticMean(array: [Double]) -> Double {
    var total: Double = 0
    for number in array {
        total += number
    }
    return total / Double(array.count)
}

// Function to calculate the standard deviation

func standardDeviation(array: [Double]) -> Double
{
    let length = Double(array.count)
    let avg = array.reduce(0, {$0 + $1}) / length
    let sumOfSquaredAvgDiff = array.map { pow($0 - avg, 2.0)}.reduce(0, {$0 + $1})
    return sqrt(sumOfSquaredAvgDiff / length)
}

// Function to extract some range from an array

func subArray<T>(array: [T], s: Int, e: Int) -> [T] {
    if e > array.count {
        return []
    }
    return Array(array[s..<min(e, array.count)])
}

// Smooth z-score thresholding filter

func ThresholdingAlgo(y: [Double],lag: Int,threshold: Double,influence: Double) -> ([Int],[Double],[Double]) {

    // Create arrays
    var signals   = Array(repeating: 0, count: y.count)
    var filteredY = Array(repeating: 0.0, count: y.count)
    var avgFilter = Array(repeating: 0.0, count: y.count)
    var stdFilter = Array(repeating: 0.0, count: y.count)

    // Initialise variables
    for i in 0...lag-1 {
        signals[i] = 0
        filteredY[i] = y[i]
    }

    // Start filter
    avgFilter[lag-1] = arithmeticMean(array: subArray(array: y, s: 0, e: lag-1))
    stdFilter[lag-1] = standardDeviation(array: subArray(array: y, s: 0, e: lag-1))

    for i in lag...y.count-1 {
        if abs(y[i] - avgFilter[i-1]) > threshold*stdFilter[i-1] {
            if y[i] > avgFilter[i-1] {
                signals[i] = 1      // Positive signal
            } else {
                // Negative signals are turned off for this application
                signals[i] = -1       // Negative signal
            }
            filteredY[i] = influence*y[i] + (1-influence)*filteredY[i-1]
        } else {
            signals[i] = 0          // No signal
            filteredY[i] = y[i]
        }
        // Adjust the filters
        avgFilter[i] = arithmeticMean(array: subArray(array: filteredY, s: i-lag, e: i))
        stdFilter[i] = standardDeviation(array: subArray(array: filteredY, s: i-lag, e: i))
    }
    return (signals,avgFilter,stdFilter)
}


func viewDidLoad()
{

// Demo

let samples = [0.01, -0.02, -0.02, 0.01, -0.01, -0.01, 0.00, 0.10, 0.31,
               -0.10, -0.73, -0.68, 0.21, 1.22, 0.67, -0.59, -1.04, 0.06, 0.42, 0.07,
               0.03, -0.18, 0.11, -0.06, -0.02, 0.16, 0.21, 0.03, -0.68, -0.89, 0.18,
               1.31, 0.66, 0.07, -1.62, -0.16, 0.67, 0.19, -0.42, 0.23, -0.05, -0.01,
               0.03, 0.06, 0.27, 0.15, -0.50, -1.18, 0.11, 1.30, 0.93, 0.16, -1.32,
               -0.10, 0.55, 0.23, -0.03, -0.23, 0.16, -0.04, 0.01, 0.12, 0.35, -0.38,
               -1.11, 0.07, 1.46, 0.61, -0.68, -1.16, 0.29, 0.54, -0.05, 0.02, -0.01,
               0.12, 0.23, 0.29, -0.75, -0.95, 0.11, 1.51, 0.70, -0.30, -1.48, 0.13,
               0.50, 0.18, -0.06, -0.01, -0.02, 0.03, -0.02, 0.06, 0.03, 0.03, 0.02,
               -0.01, 0.01, 0.02, 0.01]

// Run filter

let (signals,avgFilter,stdFilter) = ThresholdingAlgo(y: samples, lag: 10, threshold: 3, influence: 0.2)

// Print output to console
print("\nOutput: \n ")

    for i in 0...signals.count - 1
    {
        print("Data point \(i)\t\t sample: \(samples[i]) \t signal: \(signals[i])\n")
    }

    // Raw data for creating a plot in Excel
    print("\n \n Raw data for creating a plot in Excel: \n ")
    for i in 0...signals.count - 1
    {
        print("\(i+1)\t\(samples[i])\t\(signals[i])\t\(avgFilter[i])\t\(stdFilter[i])\n")
    }
}

Your help is much appreciated! I have added in the entire code so this might help to debug the issue.

2
  • You should print the value of signals.count prior to your print statements. Perhaps it is zero, in which case nothing prints. But we don't know what it is. Commented Oct 19, 2017 at 7:55
  • I tried this method but yet to solve the issue. Thank you for your response though. Commented Oct 19, 2017 at 8:15

2 Answers 2

1

Ok maybe I found Your problem. Function viewDidLoad in not in any class, and it does not overide any delegate methods. That means it is not called automatically when the view appears.

Example

class ViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad() // the view did load gets called when view appears
    // Do any additional setup after loading the view, typically from a nib.
}
}

In this case our class loads when the view is displayed, but You function viewDidLoad is not inside any class.

Sign up to request clarification or add additional context in comments.

Comments

0

Probably the signals array is empty, so the loop does not get executed. Maybe You wanted to put "samples.cout-1" instead of "signals"?

1 Comment

I tried This method as well but the result is the same. Thank you anyways.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.