-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab1.swift
executable file
·154 lines (133 loc) · 3.75 KB
/
lab1.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// main.swift
// test-swift
//
// Created by niquit on 07/10/2017.
// Copyright © 2017 niquit. All rights reserved.
//
import Foundation
var A: [Float] = []
var a: String? = nil
var Ω: [Float] = []
var ω: String? = nil
var Φ: [Float] = []
var φ: String? = nil
var f: String? = nil
var o: String! = "+"
var P: String? = nil
var p: Double! = 500
func Help() {
print("\u{001B}[1;97mUSAGE: Sample Tone by niquit, version X")
print("\u{001B}[1;94m-a amplituda {2}{2,3}")
print("\u{001B}[1;94m-w omega {2}{2,3}")
print("\u{001B}[1;94m-f częstotliwość {20}{20,30}")
print("\u{001B}[1;94m-F przesunięcie {2}{2,3}")
print("\u{001B}[1;94m-o operacja {+}{-}{*}")
print("\u{001B}[1;94m-p próbkowanie {100}{500}")
}
while case let option = getopt(CommandLine.argc, CommandLine.unsafeArgv, "a:w:F:f:o:p:h"), option != -1 {
switch UnicodeScalar(CUnsignedChar(option)) {
case "a":
a = String(cString: optarg)
case "w":
ω = String(cString: optarg)
case "F":
φ = String(cString: optarg)
case "f":
f = String(cString: optarg)
case "o":
o = String(cString: optarg)
case "p":
P = String(cString: optarg)
p = Double(P!)
case "h":
Help()
exit(0)
default:
Help()
exit(1)
}
}
if ((a ?? "").isEmpty) || (((ω ?? "").isEmpty) && ((f ?? "").isEmpty)) || ((φ ?? "").isEmpty) {
Help()
exit(1)
}
//Amplituda
for i in a!.components(separatedBy:",") {
A.append(Float(i)!)
}
//Częstotliwość
if (ω ?? "").isEmpty {
for i in f!.components(separatedBy:",") {
Ω.append(2*Float.pi*Float(i)!)
}
}
else {
for i in ω!.components(separatedBy:",") {
Ω.append(Float(i)!)
}
}
//Przesunięcie
for i in φ!.components(separatedBy:",") {
Φ.append(Float(i)!)
}
//Okres ( próbkowanie )
var Times:[Float] = []
p = 1/p
for i in stride(from: 0.00, to: 1.00, by: p) {
Times.append(Float(i))
}
//Ton prosty
var ax: [Float] = []
var Tones:[Float] = []
for i in Times {
if (A.count) > 1 && (Ω.count) > 1 && (Φ.count) > 1 && o == "+" {
Tones.append((A[0] * sin(Ω[0] * i + Φ[0])) + (A[1] * sin(Ω[1] * i + Φ[1])))
}
else if (A.count) > 1 && (Ω.count) > 1 && (Φ.count) > 1 && o == "-" {
Tones.append((A[0] * sin(Ω[0] * i + Φ[0])) - (A[1] * sin(Ω[1] * i + Φ[1])))
}
else if (A.count) > 1 && (Ω.count) > 1 && (Φ.count) > 1 && o == "*" {
Tones.append((A[0] * sin(Ω[0] * i + Φ[0])) * (A[1] * sin(Ω[1] * i + Φ[1])))
}
else {
Tones.append(A[0] * sin(Ω[0] * i + Φ[0]))
}
ax.append(Float(i))
}
//CSV
let DocumentDirURL: String! = "/Users/niquit/WIZUT/PTD/lab1/"
//Ton prosty
var fileName = "lab1.1"
var fileURL = URL(fileURLWithPath: DocumentDirURL).appendingPathComponent(fileName).appendingPathExtension("csv")
var fileText: String = ""
for index in 1...(Times.count) {
fileText+="\(Tones[index-1])"
if (( index < (Times.count) )) {
fileText+=","
}
}
var writeString = fileText
do {
try writeString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
}
catch let error as NSError {
print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
}
//OŚ X
fileName = "lab1.2"
fileURL = URL(fileURLWithPath: DocumentDirURL).appendingPathComponent(fileName).appendingPathExtension("csv")
fileText = ""
for index in 1...(Times.count) {
fileText+="\(ax[index-1])"
if (( index < (Times.count) )) {
fileText+=","
}
}
writeString = fileText
do {
try writeString.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
}
catch let error as NSError {
print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
}