Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a simple function that reads arrays instead of a file #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

BrandonRaeder
Copy link

Fairly simple. Just figured I add it if someone else wanted it. May not be be the right approach. But, nice library! Cant wait to learn more about it!

Added array insert instead of file.
Read an arrays instead of a file.
io.go Outdated
@@ -31,3 +31,16 @@ func ReadSignalFile(path string, sampleRate float64) (*Signal, error) {
}
return &signal, scanner.Err()
}
// Function allows for reading arrays
func ReadArray(dataArray []float64, sampleRate float64) (*Signal, error){
Copy link
Owner

@eripe970 eripe970 Jul 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you need to copy the array or can it be like below?

signal := Signal{
		SampleRate: sampleRate,
		Signal:   dataArray,
	}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah instantiating it would work also!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have to call the type from a different part of the application to update Signal var. I guess totally do able.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! If you make the change then Ill mergar this PR :)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am pretty sure i pushed to the right folder! Been a long week, sorry for the delay.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something like this should work,

func CreateSignalFromArray(signal []float64, sampleRate float64) *Signal {
	return &Signal{
		SampleRate: sampleRate,
		Signal:     signal,
	}
}
  • Replaced the return with &Signal instead of (&Signal, error) since there isn't any error returned
  • You can use the array directly, no need to copy the values.
  • Changed to name "signal" since I think the go-lang best practice is not to have type name on variables (like dataArray).

This is using the same array as the one that is inserted, if you want to use a deep copy you can use something like

cpy := make([]float64, len(data))
copy(cpy, data)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants