Finding Index for 0Hz on RF Spectrum

In summary, the conversation discusses finding the index corresponding to 0Hz on an RF spectrum vs frequency graph using the np.argmax or np.argmin equations. One solution suggested is to iterate through the array and keep track of the index when the array value is 0. An example code is provided, but the student is having trouble implementing it with their specific array. Suggestions are made to iterate over the existing array rather than creating a new one.
  • #1
Erenjaeger
141
6

Homework Statement


Find the index corresponding to 0Hz on my RF spectrum vs frequency garph

Homework Equations


using np.argmax or np.argmin [/B]

The Attempt at a Solution


np.argmin just gives the proof that the lowest value in frequency array plotted on x-axis is 0, but now i need to find the index in the array where this happens so i can set the RF spectrum to 0 at that value in order to determine frep or a laser. [/B]
 
Physics news on Phys.org
  • #2
Erenjaeger said:

Homework Statement


Find the index corresponding to 0Hz on my RF spectrum vs frequency garph

Homework Equations


using np.argmax or np.argmin [/B]

The Attempt at a Solution


np.argmin just gives the proof that the lowest value in frequency array plotted on x-axis is 0, but now i need to find the index in the array where this happens so i can set the RF spectrum to 0 at that value in order to determine frep or a laser. [/B]
in python 3 btw
 
  • #3
Erenjaeger said:

Homework Statement


Find the index corresponding to 0Hz on my RF spectrum vs frequency garph

Homework Equations


using np.argmax or np.argmin [/B]

The Attempt at a Solution


np.argmin just gives the proof that the lowest value in frequency array plotted on x-axis is 0, but now i need to find the index in the array where this happens so i can set the RF spectrum to 0 at that value in order to determine frep or a laser. [/B]
Can't you just iterate through the array, keeping track of what index you're on, and see when the array value is 0?
 
  • #4
Mark44 said:
Can't you just iterate through the array, keeping track of what index you're on, and see when the array value is 0?
Not sure how you would code that though I have to write a piece of code that does it, seems simple but I can't find any information on how to find the index where frequency is 0
 
  • #5
Here's an example of what I'm talking about:
Python:
import array

arr0 = array.array('i', [3, 17, -1, 0, 5, 23, 18])
found = False

for i in range(len(arr0)):
   if arr0[i] == 0:
      found = True
      print("Found 0 at index ", i)
if found == False:
   print("Could not find 0")
If one of the values in the array happens to be zero, this code prints the corresponding index where it was found. If the array doesn't contain 0, the code displays a message that zero couldn't be found.
 
  • #6
Mark44 said:
Here's an example of what I'm talking about:
Python:
import array

arr0 = array.array('i', [3, 17, -1, 0, 5, 23, 18])
found = False

for i in range(len(arr0)):
   if arr0[i] == 0:
      found = True
      print("Found 0 at index ", i)
if found == False:
   print("Could not find 0")
If one of the values in the array happens to be zero, this code prints the corresponding index where it was found. If the array doesn't contain 0, the code displays a message that zero couldn't be found.
The array is stored under a variable "f2" which is the x-axis and then "RF signal" which is the y axis, I tried running your code with f2 as the array but it didn't run. any idea why that might have been?
 
  • #7
Can you show the code and the error message (or whatever was the result of running the code)?
 
  • #8
mfb said:
Can you show the code and the error message (or whatever was the result of running the code)?
Yep! attached is the screen shot and also I printed the array "f2" so you could get an idea of what it consists of.
22883489_1503914236355013_1819086435_n.png
 

Attachments

  • 22883489_1503914236355013_1819086435_n.png
    22883489_1503914236355013_1819086435_n.png
    25 KB · Views: 722
  • #9
Erenjaeger said:
Yep! attached is the screen shot and also I printed the array "f2" so you could get an idea of what it consists of.
View attachment 213956
I thought it might be taking f2 as a single element of the array rather than an array itself because that error message about length-1 arrays?
 
  • #10
What exactly do you expect "arr0=array.array('i',[f2])" to do? Why don't you iterate over f2 and get rid of arr0?
 
  • #11
In my example, I created the array named arr0. This array contains integers, which is the reason for 'i' in the statement arr0 = array.array('i', ...). Since you already have an array, f2, you don't need to create one. As @mfb suggests, just iterate over the array you have.
 
  • Like
Likes Erenjaeger

What is an index in an array?

An index in an array is a numerical value that represents the position of an element within the array. It is used to access and manipulate specific elements in the array.

How do you find the index of an element in an array?

The index of an element in an array can be found by using the indexOf() method. This method takes in the value of the element as an argument and returns the index of the first occurrence of that element in the array. If the element is not found, it returns -1.

Can you find the index of multiple occurrences of an element in an array?

Yes, the indexOf() method only returns the index of the first occurrence of an element. To find the index of all occurrences, you can use a for loop and check for each index using the indexOf() method.

What is the time complexity of finding an index in an array?

The time complexity of finding an index in an array using the indexOf() method is O(n), where n is the length of the array. This means that the time taken to find the index increases linearly with the size of the array.

Are there other ways to find the index of an element in an array?

Yes, there are other methods such as findIndex(), lastIndexOf(), and includes() that can be used to find the index of an element in an array. Each method has its own advantages and use cases, so it's important to choose the most appropriate one for your specific situation.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Introductory Physics Homework Help
Replies
24
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Atomic and Condensed Matter
Replies
8
Views
2K
Back
Top